c# - Workaround to avoid "Cannot create default converter to perform 'two-way' conversions between types 'Derived_N' and 'Base'" error -
i have types hierarchy:
public class base {} public class derived_1 : base {} public class derived_2 : base {} // more descendants... public class derived_n : base {}
types hierarchy used lookup lists in view models:
public class someviewmodel { // available items public ienumerable<derived_n> someitems { get; } // selected item public derived_n selecteditem { get; set; } // there several property pairs above }
to select values lookup list i've created user control (some sort of selector). since point of selection process base
descendants looks similar, user control operates base
type properties:
public ienumerable<base> itemssource { { return (ienumerable<base>)getvalue(itemssourceproperty); } set { setvalue(itemssourceproperty, value); } } public static readonly dependencyproperty itemssourceproperty = dependencyproperty.register("itemssource", typeof(ienumerable<base>), typeof(baseselector), new propertymetadata(null)); public base selecteditem { { return (base)getvalue(selecteditemproperty); } set { setvalue(selecteditemproperty, value); } } public static readonly dependencyproperty selecteditemproperty = dependencyproperty.register("selecteditem", typeof(base), typeof(baseselector), new frameworkpropertymetadata(null, frameworkpropertymetadataoptions.bindstwowaybydefault));
xaml looks like:
<myctrls:baseselector itemssource="{binding someitems}" selecteditem="{binding selecteditem}"/>
this works expected, there binding errors this:
cannot create default converter perform 'two-way' conversions between types 'derived_n' , 'base'
i know, why in output window - in theory, selecteditem
type, derived base
, in practice not case. error disappears, if converter:
public class dummyconverter : ivalueconverter { public object convert(object value, type targettype, object parameter, cultureinfo culture) { return value; } public object convertback(object value, type targettype, object parameter, cultureinfo culture) { return value; } }
is used in binding:
<myctrls:baseselector itemssource="{binding someitems}" selecteditem="{binding selecteditem, converter={staticresource dummyconverterkey}}"/>
but don't want use @ - can see, there no payload in converter (while there lot such of properties).
are there other workaround?
for now, i've solved problem replacing property type user control's properties ienumerable
/ object
respectively (ienumerable<object>
/ object
solution):
public static readonly dependencyproperty itemssourceproperty = dependencyproperty.register("itemssource", typeof(ienumerable), typeof(baseselector), new propertymetadata(null)); public static readonly dependencyproperty selecteditemproperty = dependencyproperty.register("selecteditem", typeof(object), typeof(baseselector), new frameworkpropertymetadata(null, frameworkpropertymetadataoptions.bindstwowaybydefault));
this leads type checking inside user control, doesn't generate binding errors (and don't understand, why - case object
same base
, imo).
Comments
Post a Comment