Created
October 13, 2011 06:39
-
-
Save philipmat/1283578 to your computer and use it in GitHub Desktop.
DataBinding errors with interfaces
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
private object FormatObject(object value) | |
{ | |
if (this.ControlAtDesignTime()) | |
return value; | |
System.Type propertyType = this.propInfo.PropertyType; | |
if (this.formattingEnabled) | |
{ | |
ConvertEventArgs cevent = new ConvertEventArgs(value, propertyType); | |
this.OnFormat(cevent); | |
if (cevent.Value != value) | |
return cevent.Value; | |
TypeConverter sourceConverter = (TypeConverter) null; | |
if (this.bindToObject.FieldInfo != null) | |
sourceConverter = this.bindToObject.FieldInfo.Converter; | |
return Formatter.FormatObject(value, propertyType, sourceConverter, this.propInfoConverter, this.formatString, this.formatInfo, this.nullValue, this.dsNullValue); | |
} | |
else | |
{ | |
ConvertEventArgs cevent = new ConvertEventArgs(value, propertyType); | |
this.OnFormat(cevent); | |
object obj1 = cevent.Value; | |
if (propertyType == typeof (object)) | |
return value; | |
if (obj1 != null && (obj1.GetType().IsSubclassOf(propertyType) || obj1.GetType() == propertyType)) | |
return obj1; | |
TypeConverter converter = TypeDescriptor.GetConverter(value != null ? value.GetType() : typeof (object)); | |
if (converter != null && converter.CanConvertTo(propertyType)) | |
return converter.ConvertTo(value, propertyType); | |
if (value is IConvertible) | |
{ | |
object obj2 = Convert.ChangeType(value, propertyType, (IFormatProvider) CultureInfo.CurrentCulture); | |
if (obj2 != null && (obj2.GetType().IsSubclassOf(propertyType) || obj2.GetType() == propertyType)) | |
return obj2; | |
} | |
throw new FormatException(System.Windows.Forms.SR.GetString("ListBindingFormatFailed")); | |
} | |
} | |
... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
to use with LINQPad : http://www.linqpad.net | |
add reference to the following: | |
System.Windows.Forms | |
System.ComponentModel | |
System.Globalization | |
System.Windows.Forms | |
*/ | |
void Main() | |
{ | |
MyForm viewBase = new MyForm { Text = "Base" }, viewDerived = new MyForm { Text = "Derived" }; | |
var viewModel = new MyViewModel(); | |
viewBase.DataBindings.Add("Base", viewModel, "Base", false, DataSourceUpdateMode.OnPropertyChanged); | |
viewDerived.DataBindings.Add("Derived", viewModel, "Derived", false, DataSourceUpdateMode.OnPropertyChanged); | |
viewBase.Show(); | |
viewDerived.Show(); | |
} | |
// Define other methods and classes here | |
interface IBase { | |
string BaseMethod(); | |
} | |
interface IDerived : IBase { | |
string DerivedMethod(); | |
} | |
class Base: IBase { | |
public string BaseMethod() {return "BaseMethod"; } | |
} | |
[TypeConverter(typeof(DerivedConverter))] | |
class Derived : Base, IDerived { | |
public string DerivedMethod(){return "DerivedMethod"; } | |
} | |
class DerivedConverter : TypeConverter { | |
private readonly Type typeD = typeof(Derived); | |
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) { | |
if (destinationType.IsAssignableFrom(typeD)) return true; | |
return base.CanConvertFrom(context, destinationType); | |
} | |
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, | |
object value, Type destinationType) { | |
if (destinationType.IsAssignableFrom(typeD)) return value; | |
return base.ConvertTo(context, culture, value, destinationType); | |
} | |
} | |
class MyForm : Form { | |
public IBase Base { get; set; } | |
public IDerived Derived { get ; set; } | |
protected override void OnLoad(EventArgs e) { | |
(Base == null ? "Base is null" : Base.BaseMethod()).Dump("MyForm." + this.Text); | |
(Derived == null ? "Derived is null" : Derived.DerivedMethod()).Dump("MyForm." + this.Text); | |
} | |
} | |
class MyViewModel { | |
public IBase Base { get { return new Base(); } } | |
public IDerived Derived { get { return new Derived(); } } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Full stack trace: | |
System.FormatException: Cannot format the value to the desired type. | |
at System.Windows.Forms.Binding.FormatObject(Object value) | |
at System.Windows.Forms.Binding.PushData(Boolean force) | |
at System.Windows.Forms.Binding.UpdateIsBinding() | |
at System.Windows.Forms.Binding.CheckBinding() | |
at System.Windows.Forms.Binding.SetListManager(BindingManagerBase bindingManagerBase) | |
at System.Windows.Forms.ListManagerBindingsCollection.AddCore(Binding dataBinding) | |
at System.Windows.Forms.BindingsCollection.Add(Binding binding) | |
at System.Windows.Forms.BindingContext.UpdateBinding(BindingContext newBindingContext, Binding binding) | |
at System.Windows.Forms.Control.UpdateBindings() | |
at System.Windows.Forms.Control.OnBindingContextChanged(EventArgs e) | |
at System.Windows.Forms.Control.set_BindingContextInternal(BindingContext value) | |
at System.Windows.Forms.ContainerControl.set_BindingContext(BindingContext value) | |
at System.Windows.Forms.ContainerControl.get_BindingContext() | |
at System.Windows.Forms.Control.UpdateBindings() | |
at System.Windows.Forms.Control.OnBindingContextChanged(EventArgs e) | |
at System.Windows.Forms.ContainerControl.OnCreateControl() | |
at System.Windows.Forms.Form.OnCreateControl() | |
at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible) | |
at System.Windows.Forms.Control.CreateControl() | |
at System.Windows.Forms.Control.WmShowWindow(Message& m) | |
at System.Windows.Forms.Control.WndProc(Message& m) | |
at System.Windows.Forms.ScrollableControl.WndProc(Message& m) | |
at System.Windows.Forms.Form.WmShowWindow(Message& m) | |
at System.Windows.Forms.Form.WndProc(Message& m) | |
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) | |
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) | |
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment