简体   繁体   English

如何添加具有离散字符串值集的依赖项属性?

[英]How do you add a dependency property with a discrete set of string values?

Many properties in standard WPF elements support a small discrete set of values that can be specified as strings when the elements are created in XAML. 标准WPF元素中的许多属性都支持一组小的离散值,当在XAML中创建元素时,这些值可以指定为字符串。 For example, StackPanel's Orientation property supports "Horizontal" and "Vertical" as discrete properties. 例如,StackPanel的Orientation属性支持“ Horizo​​ntal”和“ Vertical”作为离散属性。 IntelliSense aids you by giving you these two options after you open the quotes for the Orientation property. 在打开“方向”属性的引号后,IntelliSense会为您提供这两个选项,从而为您提供帮助。 I know a value converter is involved in converting these to enum values of type System.Windows.Controls.Orientation enum. 我知道值转换器涉及将这些转换为System.Windows.Controls.Orientation枚举类型的枚举值。

How do you do this for a custom dependency property for your custom control? 如何为自定义控件的自定义依赖项属性执行此操作? I want it to work just as it does for standard element dependency properties with the IntelliSense help and all. 我希望它能像使用IntelliSense帮助及其他所有标准元素依赖项属性一样工作。

Thanks 谢谢

If they won't change at run time, the answer is enum . 如果它们在运行时不会更改,则答案为enum If they do change, the way I do them is with strings (or objects that implement ToString() ) and converters that fill in the possible values. 如果它们确实发生了变化,则我的处理方式是使用字符串(或实现ToString()对象)以及填充可能值的转换器。

Incidentally this is the same way the normal WinForms property grid works. 顺便说一句,这与普通WinForms属性网格的工作方式相同。

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

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