XAML - 逗号分隔的依赖属性
我有一个名为 AppPreferences 的自定义类。此类有一个名为 Color 的依赖属性。此依赖项属性表示 Colors 类型的枚举值(这是一个自定义枚举器)。我的 AppPreferences 代码如下所示:
public class AppPreferences
{
public static readonly DependencyProperty ColorProperty = DependencyProperty.RegisterAttached(
"Color",
typeof(MyServiceProxy.Colors),
typeof(AppPreferences),
new PropertyMetadata(MyServiceProxy.Colors.DEFAULT, new PropertyChangedCallback(OnColorChanged))
);
private static void OnColorChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
// Do Stuff
}
}
作为开发人员,我将其添加到我的 UI 元素中以帮助确定颜色。例如,我会做这样的事情:
<TextBox custom:AppPreferences.Color="Black" ... />
我现在需要支持后备颜色。换句话说,我希望能够提供类似于以下内容的逗号分隔的颜色值列表:
<TextBox custom:AppPreferences.Color="Black,Blue" ... />
我的问题是,如何更新我的依赖项属性和 OnColorChanged 事件处理程序以支持多个值?
谢谢你!
I have a custom class called AppPreferences. This class has a dependency property called Color. This dependency property represents an enumerated value of the type Colors (which is a custom enumerator). My code for AppPreferences is shown here:
public class AppPreferences
{
public static readonly DependencyProperty ColorProperty = DependencyProperty.RegisterAttached(
"Color",
typeof(MyServiceProxy.Colors),
typeof(AppPreferences),
new PropertyMetadata(MyServiceProxy.Colors.DEFAULT, new PropertyChangedCallback(OnColorChanged))
);
private static void OnColorChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
// Do Stuff
}
}
As a developer, I add this to my UI elements to help determine the color. For instance, I'll do something like this:
<TextBox custom:AppPreferences.Color="Black" ... />
I now have a need to support fallback colors. In other words, I want to be able to provide a comma-delimited list of Colors values similar to the following:
<TextBox custom:AppPreferences.Color="Black,Blue" ... />
My question is, how do I update my dependency property and OnColorChanged event handler to support multiple values?
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您试图实现的机制称为“附加属性”。
有关信息,请阅读此。
以下是完成所有操作的简短代码摘录:
阅读此 以获取有关 Xaml 中逗号分隔枚举的更多信息。
您还可能需要查看此。
The mechanism you're trying to achieve is called "Attached properties".
Read this for info.
Here is a short code excerpt that does it all:
Read this to get more on comma-separated enumerations in Xaml.
Also you may want checkout this.
您应该确保您有一个flagwise枚举以便允许这个语法。这可以通过添加
FlagsAttribute
到您的枚举。You should ensure that you have a flagwise enumeration in order to allow this syntax. This is possible by adding the
FlagsAttribute
to your enumeration.