XAML - Comma Separated Dependency Property

I have a custom class called AppPreferences. This class has the Color dependency property. This dependency property is an enumerated value of type Colors (which is a regular 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 user interface elements to define color. For example, I will do something like this:

 <TextBox custom:AppPreferences.Color="Black" ... /> 

Now I need to maintain backup colors. In other words, I want to have a comma-separated list of Color values, similar to the one below:

 <TextBox custom:AppPreferences.Color="Black,Blue" ... /> 

My question is: how to update the dependency property and OnColorChanged event handler to support multiple values?

Thanks!

+4
source share
2 answers

The mechanism you are trying to achieve is called "Attached properties".

Read this for information.

Here is a brief snippet of code that does everything:

 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 one to get more about enumerated commas in Haml.

You can also check.

0
source

You must ensure that you have a flag enumeration to enable this syntax. This is possible by adding FlagsAttribute to your listing.

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

For flag enumerations, the behavior is based on the Enum.Parse method. You can specify several values ​​for listing by flags, separating each value with a comma. However, you cannot combine values ​​that are not flagships . For example, you cannot use comma syntax to try to create a trigger that acts on several non-flag enumeration conditions.

0
source

Source: https://habr.com/ru/post/1391466/


All Articles