UserControl枚举DependencyProperty不绑定

时间:2014-03-24 07:10:04

标签: wpf user-controls enums dependency-properties

我创建了一个包含3个DependencyProperties的UserControl。两个工作正常,但有一个让我真正头痛。 我有一个枚举(在UserControl类之外但是相同的命名空间):

public enum RecordingType
{
    NoRecording,
    ContinuesRecording,
    EventRecording
}

我为它创建了一个DependencyProperty,如下所示:

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);
        }
    }

我正在XAML中使用它,如下所示:

<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}"/>

还有两个DependencyProperties可以正常工作(我在UserControl中获取它们的get和set方法),但这个只是一个禁忌。我之前创建了DP,并且我做了同样的事情。我还确保我的VM中的绑定正常,并且正确调用了getter和setter。

任何帮助都会很棒!

我还检查了我的VM。绑定确实执行。

1 个答案:

答案 0 :(得分:0)

让我展示UserControl(从现在开始的UC)与ComboBoxEnum绑定的其他解决方案。 也是一个常见的问题,当你可以绑定枚举时,但你无法从UC获得SelectedItem的{​​{1}}。此解决方案还将提供ComboBox

例如,我有一个SelectedItem UC类,它可以接受枚举,并提供ExampleUC : UserControl。它将使用属性(.xaml中的属性)来完成它。 我还有一个名为SelectedItem的枚举和ExampleEnum,它会创建Window的新实例并设置该属性/属性。

在ExampleUC.xaml中:

ExampleUC

正如您所看到的,UC的<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,这意味着它可以接收所需的参数(您可以找到有关DataContext继承的更多解释)和视觉树,只是对它们进行一些研究。)

绑定属性(EnumTypeArray和SelectedItem)是ExampleUC.xaml.cs文件中的DependencyProperties:

DataContext

要创建新的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代码段。 (写下它并默认按TAB键)。当您创建和编辑ExampleUC的实例时,这些属性将在.xaml编辑器中显示为属性。

在此阶段,您有一个可以接受枚举的UC,并返回propdp

某处的枚举:

SelectedItem

public enum ExampleEnum { Example1, Example2 } ,它使用ExampleUC:

您必须向Window的资源添加一个新资源,该资源为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> 名称空间前缀之前已在名称空间中定义了&#39; section,local的命名空间,例如:

ExampleEnum

要在xmlns:local="clr-namespace:TestNamespace.Data"ExampleUC中使用Grid,请使用以下内容:

Panel

要将<views:ExampleUC EnumTypeArray="{Binding Source={StaticResource MyEnumName}}" SelectedItem="{Binding MyProperty, Mode=TwoWay}"/> 设置为Mode,必须能够TwoWayget财产。 请注意,您可能必须在命名空间中定义set命名空间&#39;如果Visual Studio不能为你做这个部分。

如您所见,先前定义的DependencyProperties显示为属性。 views负责填充ComboBox的项目,EnumTypeArray已绑定到MyProperty,这是模型类中的属性,例如:

SelectedItem

此示例仅显示如何通过UC使用枚举。由于这个UC只有一个组件(public ExampleEnum MyProperty{ get{ return _myProperty;} set{ _myProperty = value; OnPropertyChanged("MyProperty"); } } ),在实践中它没用。如果你用ComboBox或其他人来装饰它,那就可以胜任。

希望它有所帮助。