简体   繁体   English

将CustomControl DependencyProperty绑定到ViewModel枚举

[英]Binding a CustomControl DependencyProperty to a ViewModel enum

I'm attempting to create a CustomControl which will have various properties affected by an enum found in a ViewModel. 我试图创建一个CustomControl,它将具有受ViewModel中的枚举影响的各种属性。 The CustomControl created will be called within a View. 创建的CustomControl将在视图中调用。

I would like the enum binding to change a couple of things within the Control, these include: 我希望枚举绑定可以更改控件中的几项内容,包括:

  • Changing an Images source (images changed to are static within a folder created inside the CustomControl Library), 更改图像源(在CustomControl库内部创建的文件夹中,更改为静态图像),
  • Alter a TextBlocks Text property, again, the text options will be static between one of X amount of cases (X being the number of possible enum states(also doesn't change)). 再次更改TextBlocks Text属性,文本选项在X种情况之一(X是可能的枚举状态数(也不变))之间将是静态的。

Keep in mind, these two DP's will be linked eg if bound enum is case x, the image source will change and the displayed text. 请记住,这两个DP将链接在一起,例如,如果绑定枚举为case x,则图像源将更改并且显示的文本。 Basically, the two exposed DP's here will be bound to the same enum , but use it differently. 基本上,这里公开的两个DP将绑定到相同的枚举 ,但使用方式不同。


So, my question is, what would be the best way to carry what is described above? 因此,我的问题是,携带上述物品的最佳方法是什么?

I've already thought about two possible ways: 我已经考虑过两种可能的方法:

  1. Expose useful DP types and bind these properties to the same enum (eg Property1="{Binding someEnum}" Property2="{Binding someEnum}") and also provide a ValueConvertor to convert these enums to useful types which the CustomControl can then use, 公开有用的DP类型并将这些属性绑定到相同的枚举(例如,Property1 =“ {Binding someEnum}” Property2 =“ {Binding someEnum}”),并提供一个ValueConvertor将这些枚举转换为CustomControl可以使用的有用类型,
  2. Or, expose a single DP which accepts an enum and then changes the properties somewhere else within the CustomControl code, depending on the value of the bound enum? 还是公开一个可以接受枚举的DP,然后根据绑定枚举的值更改CustomControl代码中其他位置的属性?

I am fairly comfortable with carrying out option 1 with a value convertor in the Views code-behind file. 我对在Views代码隐藏文件中使用值转换器执行选项1感到很满意。 However, I would like to avoid doing it this way as it would make the control less independant. 但是,我想避免这样做,因为这会使控件的独立性降低。

Regarding option two, I'm not entirely sure how to do it this way so articles or a small explanation would help if you feel this is the way to go - Possibly implementing INoitifyPropertyChanged on the DP so the control knows it's value has been changed when the Bound enum changes? 关于选项二,我不完全确定该怎么做,因此,如果您觉得这是可行的方法,那么文章或少量解释会有所帮助-可能在DP上实现INoitifyPropertyChanged,因此控件知道它的值已更改绑定的枚举发生变化吗?

Also, I am not limited to doing it the two ways described, if you feel there is a better way, please suggest it! 另外,我不仅限于上述两种方法,如果您觉得有更好的方法,请提出建议!

I am using WPF (C#) in conjunction with MVVM architecture (MVVM Light Toolkit). 我将WPF(C#)与MVVM体系结构(MVVM Light Toolkit)结合使用。

Quick Note: I would like to avoid altering the ViewModel - The exposed enum and all logic there should remain as it is unless absoloutely necessary! 快速说明:我想避免更改ViewModel-除非绝对必要,否则应保留暴露的枚举及其中的所有逻辑!

Thanks in advance for your replies and I will attempt to update this question as much as possible with responses and updates regarding personal progress with the issue as well as possible extra information which has been requested! 在此先感谢您的答复,我将尝试通过有关问题的个人进度以及要求提供的其他信息,尽可能多地更新此问题!

Both of your solutions should work just fine. 您的两种解决方案都应该可以正常工作。

For the second solution, all that you need is to register the dependecy property with a PropertyChangedCallback : 对于第二种解决方案,您所需要做的就是向PropertyChangedCallback注册Dependecy属性:

public static readonly DependencyProperty MyPropertyProperty =
        DependencyProperty.Register("MyProperty", typeof(EnumType), typeof(MyClass), new PropertyMetadata(new PropertyChangedCallback(MyPropertyChangedHandler)));

static void MyPropertyChangedHandler(DependencyObject obj, DependencyPropertyChangedEventArgs args)

Check this thread for usage of FrameworkPropertyMetadata , UIPropertyMetadata and PropertyMetadata When should I use FrameworkPropertyMetadata or UIPropertyMetadata over plain PropertyMetadata? 检查此线程是否使用FrameworkPropertyMetadataUIPropertyMetadataPropertyMetadata 何时应在普通的PropertyMetadata上使用FrameworkPropertyMetadata或UIPropertyMetadata?

The advantage of your first solution is that it's light, you only need 2 ValueConvertors , there is no need for a dependency property, so that another dev could understand there is a tight dependency between the enum and those properties just by looking at your xaml . 第一个解决方案的优点是轻巧,只需要2个ValueConvertors ,就不需要依赖项属性,这样另一个开发人员只需查看xaml就可以理解枚举和那些属性之间存在紧密的依赖项。 The disadvantage is that if you plan to use your custom control with the same logic in more than one place, copy pasting those convertors is not very neat. 缺点是,如果您打算在多个位置使用具有相同逻辑的自定义控件,则粘贴这些转换器的副本并不是很整齐。

The second solution, is a mirror to the first, you have the advantage of a greater level of reusability, but your logic gets embedded deep in your control. 第二种解决方案是第一种解决方案的镜像,您具有更高级别的可重用性的优点,但是您的逻辑已深深地嵌入到控件中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM