4

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.)

Community
  • 1
  • 1
Phil Jollans
  • 3,605
  • 2
  • 37
  • 50
  • Did you try to simply write `Value="Block"` in the Trigger? Type conversion from string to enum usually works out of the box in XAML. – Clemens Mar 18 '17 at 10:46
  • I believe you are using it correctly, but you can see Enum Converter example here: https://stackoverflow.com/questions/14279602/how-can-i-use-enum-types-in-xaml – Ricardo Serra Mar 18 '17 at 11:57
  • @RicardoSerra That answer shows a binding value converter, which should not be confused with a type converter. However, no binding is involved here. – Clemens Mar 18 '17 at 12:04
  • Thanks @Clemens. Simply writing Value="Block" works and my control is rendered correctly in the design window. I had presumed that I need to define my own TypeConverter to get this functionality. – Phil Jollans Mar 18 '17 at 12:20

1 Answers1

8

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

So you could simply write

<Trigger Value="Block" ...>
Clemens
  • 123,504
  • 12
  • 155
  • 268