【问题标题】:How to assign global enum as Tag value in XAML?如何在 XAML 中将全局枚举指定为标记值?
【发布时间】:2015-03-12 12:23:59
【问题描述】:

我阅读了有关该主题的几个问题,但答案对我不起作用。 我在 StlContainer.cs 中声明了以下枚举:

public enum ToothVisualModelType
{
    CoordinateSystemPivot = 0,
    Tooth = 1,
    Crown = 2,
    Gums = 3
}

枚举在 StlContainer 类定义之外声明,使其成为全局枚举。我想将其值分配给不同 XAML 控件的 Tag 属性,所以我尝试这样做:

<xctk:ColorPicker Tag="{x:Static local:ToothVisualModelType.Tooth}" 
                  Name="colorPickerTooth" 
                  Width="110" 
                  Grid.Column="1" 
                  Grid.Row="3" 
                  SelectedColorChanged="colorPickerTooth_SelectedColorChanged" 
                  DisplayColorAndName="True" 
                  Margin="0,0,10,5">
 </xctk:ColorPicker>

但得到了错误:

错误 1 ​​未知的构建错误,'键不能为空。 参数名称:key Line 234 Position 43.' D:\Visual Studio\Projects\Dental Viewer\Dental Viewer 1.2\Dental Viewer\MainWindow.xaml 234 43 Dental Viewer 1.2

我尝试将枚举移动到 MainWindow.xaml.cs,我尝试过

Tag="{x:Static local:StlContainer+ToothVisualModelType.Tooth}"

和

Tag="{x:Static MyNamespace:ToothVisualModelType.Tooth}"

我尝试将此分配给 Label 控件上的标记,但仍然得到相同的错误。我在这里想念什么?我可以使用某种绑定来解决这个问题吗?

PS:当我输入值并到达 Tag="{x:Static }" 时,自动完成功能只会建议 Member 参数像 Tag="{x:Static Member=}" 这样完成它,如果这很重要的话。

【问题讨论】:

  • 如何在 XAML 中定义 local 命名空间? ToothVisualModelType 在那个命名空间中吗?
  • 我看不出你的代码有什么问题 Tag="{x:Static local:ToothVisualModelType.Tooth}".. 这绝对正确...
  • @AmolBavannavar 这正是我正在与之抗争的。它应该工作,但它没有。是的,一切都在同一个命名空间中

标签: c# .net wpf xaml enums


【解决方案1】:

你的语法没问题,但是在将枚举指定为标签之前,你应该成功编译程序。

【讨论】:

【解决方案2】:

我在阅读this 后找到了解决方案。 我认为这是自动或内部完成的,但解决方案是我必须在 Window 标记中显式声明 local 命名空间,如下所示:

xmlns:local="clr-namespace:Dental_Viewer"

然后&lt;xctk:ColorPicker Tag="{x:Static local:ToothVisualModelType.Tooth}"/&gt; 就像一个魅力。

【讨论】:

    【解决方案3】:

    尝试使用这个表达式:

    <Control Name="MyControl"
             Width="100"
             Height="30">
    
        <Control.Tag>
            <x:Static Member="local:ToothVisualModelType.Tooth" />
        </Control.Tag>
    </Control>
    

    或者你可以像这样创建一个静态类:

    internal static class ToothVisualModelClass
    {
        public static string CoordinateSystemPivot = "0";
        public static string Tooth = "1";
        // ...etc...
    }
    

    在 XAML 中也这样使用:

    Tag="{x:Static local:ToothVisualModelClass.Tooth}"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-11
      • 2012-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多