c# - 在 XAML 中使用枚举值

标签 c# wpf xaml enums

我在 C# 中定义了一个枚举

public enum PointerStyle
{
  Pointer,
  Block,
  Slider
} ;

我将它用作 WPF 自定义控件的依赖属性

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); }
}

并在 ControlTemplate 中使用它

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

编辑器经常(但并非总是)在大部分代码下划线并显示错误“'Block' 不是属性 'DisplayStype' 的有效值。”如下图所示

enter image description here

这是在 Visual Studio 2015 中。

在运行时,代码运行完美。
在我的测试程序的设计窗口中,控件呈现完全错误。

我做错了什么?
在 XAML 中引用枚举值的最佳方式是什么?

(我很乐意使用 TypeConverter 并将值定义为字符串,但我找不到一个很好的例子来说明如何做。)

最佳答案

WPF 已经提供了从字符串到枚举类型的内置类型转换。

所以你可以简单地写

<Trigger Value="Block" ...>

关于c# - 在 XAML 中使用枚举值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42873051/

相关文章:

c# - WPF 列表框绑定(bind)更新

c# - 我可以使用 Microsoft Sync Framework 将本地 ms access (mdb) 数据库与 SQL Server 同步吗?

c# - 在linux上用c代码调用c#代码的最佳方法是什么

c# - 从 .ToString ("{0:C}") 格式的数字中删除 $

c# - 在 ComboBox DataTemplate 中获取 TextBlock 的值

c# - 具有已编译绑定(bind)的 HubSection

c# - 使用 DataAnnotation 将小数显示为百分比

.net - Visual Studio Launch 2010 时间的最佳利用

c# - 复选框绑定(bind)模式以一种方式工作

c# - 在组标题 WPF 中显示 "filled"行的数量