I have a type hierarchy:
public class Base {}
public class Derived_1 : Base {}
public class Derived_2 : Base {}
public class Derived_N : Base {}
Types from this hierarchy are used as search lists in models of the form:
public class SomeViewModel
{
public IEnumerable<Derived_N> SomeItems { get; }
public Derived_N SelectedItem { get; set; }
}
To select values from the search list, I created a user control (some kind of selector). Since all descendants Base
look the same from the selection process , user control controls properties of the type Base
:
public IEnumerable<Base> ItemsSource
{
get { 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
{
get { 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 usually looks like this:
<myCtrls:BaseSelector ItemsSource="{Binding SomeItems}"
SelectedItem="{Binding SelectedItem}"/>
This works as expected, but there are such errors:
Unable to create default converter for two-way conversions between types "Derived_N" and "Base"
, - SelectedItem
, Base
, .
, :
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;
}
}
:
<myCtrls:BaseSelector ItemsSource="{Binding SomeItems}"
SelectedItem="{Binding SelectedItem, Converter={StaticResource DummyConverterKey}}"/>
- , ( ).
?