UserControl enum DependencyProperty is optional

I created a UserControl that contains 3 DependencyProperties. Two work fine, but there is one that gives me a real headache. I have an enumeration (outside the UserControl class, but the same namespace):

public enum RecordingType
{
    NoRecording,
    ContinuesRecording,
    EventRecording
}

I created DependencyProperty for it as follows:

public static DependencyProperty SelectedRecordingTypeProperty = DependencyProperty.Register("SelectedRecordingType", typeof(RecordingType), typeof(SchedulerControl),
        new FrameworkPropertyMetadata((RecordingType)RecordingType.NoRecording, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));

    public RecordingType SelectedRecordingType
    {
        get
        {
            return (RecordingType)GetValue(SelectedRecordingTypeProperty);
        }
        set
        {
            SetValue(SelectedRecordingTypeProperty, value);
        }
    }

and I use it in XAML as follows:

<userControls:SchedulerControl
                        Grid.Row="1"
                        Grid.Column="3"
                        SelectedRecordingType="{Binding CurrentRecordingType,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}"
                        FullRecordingSchedule="{Binding MondayFullRecordingSchedule,UpdateSourceTrigger=PropertyChanged}"
                        SelectedRecordingTime="{Binding MondaySelectedRecordingTime,UpdateSourceTrigger=PropertyChanged}"/>

There are two more DependencyProperties that work fine (I get their get and set methods inside UserControl), but it's just not-go. I created DP before, and I do everything the same. I also made sure that the binding to my virtual machine is in order, and the receiver and setter are being called correctly.

Any help would be great!

I also verified that I am my VM. Linking is in progress.

+4
1

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 , .

, .

0

Source: https://habr.com/ru/post/1533111/


All Articles