Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using enum value in XAML

Tags:

c#

enums

wpf

xaml

I have defined an enum in C#

public enum PointerStyle
{
  Pointer,
  Block,
  Slider
} ;

I am using it as a dependency property on a WPF custom control

public static DependencyProperty DisplayStyleProperty =
    DependencyProperty.Register("DisplayStyle", typeof(PointerStyle), typeof(Pointer), new PropertyMetadata(PointerStyle.Pointer));

public PointerStyle DisplayStyle
{
  get { return (PointerStyle)GetValue(DisplayStyleProperty); }
  set { SetValue(DisplayStyleProperty, value); }
}

and using it in a ControlTemplate

<Trigger Property="DisplayStyle" Value="{x:Static local:PointerStyle.Block}">

Very often, but not always, the editor underlines most of the code and shows the error "'Block' is not a valid value for property 'DisplayStype'." as shown in the following screen shot

enter image description here

This is in Visual Studio 2015.

At runtime, the code works perfectly.
In the design window of my test program, the control is rendered totally incorrectly.

What am I doing wrong?
What is the best way to refer to an enum value in XAML?

(I would be happy to use a TypeConverter and to define the value as a string, but I can't find a good example of how do it.)

like image 793
Phil Jollans Avatar asked Feb 05 '23 20:02

Phil Jollans


1 Answers

WPF already provides built-in type conversion from string to enum types.

So you could simply write

<Trigger Value="Block" ...>
like image 103
Clemens Avatar answered Feb 11 '23 20:02

Clemens