gpt4 book ai didi

wpf - UserControl 枚举 DependencyProperty 未绑定(bind)

转载 作者:行者123 更新时间:2023-12-04 22:07:16 28 4
gpt4 key购买 nike

我创建了一个包含 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 中的绑定(bind)正常,并且正确调用了 getter 和 setter。

任何帮助都会很棒!

我还检查了我的虚拟机。绑定(bind)会执行。

最佳答案

让我展示 UserControl 的另一个解决方案(从现在开始使用 UC)带有 ComboBoxEnum绑定(bind)。也是一个常见问题,当您可以绑定(bind)枚举时,却无法获取 SelectedItem。的 ComboBox来自加州大学。此解决方案还将提供 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 继承和可视化树的解释,只需对其进行一些研究)。

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

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 键)。当您创建和编辑 ExampleUC 的实例时,这些属性将在 .xaml 编辑器中显示为属性。

在这个阶段你有一个 UC,它可以接受一个枚举,并返回 SelectedItem .

某处的枚举:

public enum ExampleEnum
{
Example1,
Example2
}

Window ,它使用了 ExampleUC:

您必须向窗口的资源中添加一个新资源,这将是一个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命名空间前缀已经在命名空间部分定义过,它是ExampleEnum的命名空间。 ,例如:

xmlns:local="clr-namespace:TestNamespace.Data"

使用ExampleUC , 在 GridPanel ,使用以下内容:

<views:ExampleUC EnumTypeArray="{Binding Source={StaticResource MyEnumName}}" SelectedItem="{Binding MyProperty, Mode=TwoWay}"/>

设置ModeTwoWay必须能够 getset该属性(property)。请注意,您可能必须定义 views命名空间部分的命名空间,如果 Visual Studio 不会为你做的话。

如您所见,先前定义的 DependencyProperties 显示为属性。 EnumTypeArray负责填充 ComboBox 的项目,SelectedItem已经绑定(bind)了MyProperty,这是模型类中的一个属性,如:

public ExampleEnum MyProperty{
get{ return _myProperty;}
set{
_myProperty = value;
OnPropertyChanged("MyProperty");
}
}

此示例仅展示如何通过 UC 使用枚举。由于此 UC 只有一个组件(ComboBox),因此在实践中毫无用处。如果你用 Label 装饰它或其他人,它会完成这项工作。

希望对您有所帮助。

关于wpf - UserControl 枚举 DependencyProperty 未绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22603041/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com