UserControl (UC ) ComboBox Enum.
, , SelectedItem ComboBox UC. SelectedItem.
, ExampleUC : UserControl UC, SelectedItem. , ( .xaml).
ExampleEnum Window, ExampleUC /.
ExampleUC.xaml:
<UserControl x:Class="TestNamespace.View.ExampleUC"
xmlns:view="clr-namespace:TestNamespace.View"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:markup="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid DataContext="{Binding RelativeSource={RelativeSource AncestorType={markup:Type view:ExampleUC}}}">
<ComboBox ItemsSource="{Binding EnumTypeArray}" SelectedItem="{Binding SelectedItem}"/>
</Grid>
</UserControl>
, DataContext UC DataContext, , ( DataContext , ).
(EnumTypeArray SelectedItem) DependencyProperties ExampleUC.xaml.cs:
public Array EnumTypeArray
{
get { return (Array)GetValue(EnumTypeArrayProperty); }
set { SetValue(EnumTypeArrayProperty, value); }
}
public object SelectedItem
{
get { return (object)GetValue(SelectedItemProperty); }
set { SetValue(SelectedItemProperty, value); }
}
public static readonly DependencyProperty EnumTypeArrayProperty =
DependencyProperty.Register("EnumTypeArray", typeof(Array), typeof(ExampleUC), new PropertyMetadata(new string[0]));
public static readonly DependencyProperty SelectedItemProperty =
DependencyProperty.Register("SelectedItem", typeof(object), typeof(ExampleUC), new PropertyMetadata(null));
DependencyProperty, propdp. ( TAB ). .xaml ExampleUC.
UC, SelectedItem.
-:
public enum ExampleEnum
{
Example1,
Example2
}
Window, ExampleUC:
Window, ObjectDataProvider, ItemsSource:
<Window.Resources>
<ObjectDataProvider x:Key="MyEnumName" MethodName="GetValues" ObjectType="{x:Type sys:Enum}">
<ObjectDataProvider.MethodParameters>
<x:Type TypeName="local:ExampleEnum"/>
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
</Window.Resources>
, local namespaces, ExampleEnum, :
xmlns:local="clr-namespace:TestNamespace.Data"
ExampleUC, Grid Panel, :
<views:ExampleUC EnumTypeArray="{Binding Source={StaticResource MyEnumName}}" SelectedItem="{Binding MyProperty, Mode=TwoWay}"/>
Mode TwoWay, get set .
, views namespaces, Visual Studio .
, DependencyProperties . EnumTypeArray ComboBox, SelectedItem MyProperty, , :
public ExampleEnum MyProperty{
get{ return _myProperty;}
set{
_myProperty = value;
OnPropertyChanged("MyProperty");
}
}
, UC. UC (a ComboBox), . Label , .
, .