XAML - 逗号分隔的依赖属性

发布于 2024-12-27 11:39:00 字数 911 浏览 5 评论 0原文

我有一个名为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

墨落画卷 2025-01-03 11:39:00

您试图实现的机制称为“附加属性”。

有关信息,请阅读

以下是完成所有操作的简短代码摘录:

public static readonly DependencyProperty IsBubbleSourceProperty = 
 DependencyProperty.RegisterAttached(
  "IsBubbleSource",
  typeof(Boolean),
  typeof(AquariumObject),
  new FrameworkPropertyMetadata(false, 
    FrameworkPropertyMetadataOptions.AffectsRender)
);
public static void SetIsBubbleSource(UIElement element, Boolean value)
{
  element.SetValue(IsBubbleSourceProperty, value);
}
public static Boolean GetIsBubbleSource(UIElement element)
{
  return (Boolean)element.GetValue(IsBubbleSourceProperty);
}

阅读 以获取有关 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:

public static readonly DependencyProperty IsBubbleSourceProperty = 
 DependencyProperty.RegisterAttached(
  "IsBubbleSource",
  typeof(Boolean),
  typeof(AquariumObject),
  new FrameworkPropertyMetadata(false, 
    FrameworkPropertyMetadataOptions.AffectsRender)
);
public static void SetIsBubbleSource(UIElement element, Boolean value)
{
  element.SetValue(IsBubbleSourceProperty, value);
}
public static Boolean GetIsBubbleSource(UIElement element)
{
  return (Boolean)element.GetValue(IsBubbleSourceProperty);
}

Read this to get more on comma-separated enumerations in Xaml.

Also you may want checkout this.

忆依然 2025-01-03 11:39:00

您应该确保您有一个flagwise枚举以便允许这个语法。这可以通过添加 FlagsAttribute 到您的枚举。

[Flags]
enum Colors
{
    Black,
    ...
}

对于逐旗枚举,行为基于 Enum.Parse
方法。您可以通过以下方式为标志枚举指定多个值
用逗号分隔每个值。但是,您不能合并
非旗标枚举值
。例如,您不能使用
尝试创建作用于多个触发器的逗号语法
非标志枚举的条件。

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.

[Flags]
enum Colors
{
    Black,
    ...
}

For flagwise enumerations, the behavior is based on the Enum.Parse
method. You can specify multiple values for a flagwise enumeration by
separating each value with a comma. However, you cannot combine
enumeration values that are not flagwise
. For instance, you cannot use
the comma syntax to attempt to create a Trigger that acts on multiple
conditions of a nonflag enumeration.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文