如何在XAML中将全局enum分配为标记值?

时间:2023-01-26 13:15:17

I read several questions on the subject, but the answers do not work for me. I have the following enum that is declared in StlContainer.cs:

我读了几个关于这个主题的问题,但答案对我不起作用。我有以下在stlcontainer中声明的enum。

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

The enum is declared outside the StlContainer class definition which makes it a global enum. I want to assign its values to the Tag property of different XAML controls, so I tried to do it like this:

enum在StlContainer类定义之外声明,这使它成为一个全局枚举。我想把它的值赋给不同XAML控件的标记属性,所以我尝试这样做:

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

But got the error:

但是有错误:

Error 1 Unknown build error, 'Key cannot be null. Parameter name: 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

错误1未知构建错误,'Key不能为空。参数名称:第234行43位。\Visual Studio\ project \Dental Viewer 1.2\牙科查看器\ \ \ \ \ \MainWindowxaml 23443牙科查看器1.2

I tried moving the enum to MainWindow.xaml.cs, I tried

我试着把enum移到MainWindow.xaml。cs,我试着

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

and

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

I tried to assign this to a Tag on a Label control and still get the same error. What am I missing here? Can I use some kind of Binding to workaround this?

我试图将它分配给标签控件上的标记,但仍然得到相同的错误。我错过了什么?我可以用某种绑定来解决这个问题吗?

PS: When I type in the value and get to Tag="{x:Static }" the autocomplete only suggests the Member parameter to complete it like this Tag="{x:Static Member=}" if that even matters.

PS:当我输入值并到达Tag="{x:Static}"时,autocomplete只建议成员参数完成它,就像这个标签="{x:Static Member=}"一样。

2 个解决方案

#1


3  

Try to use this expression:

试着用这个表达:

<Control Name="MyControl"
         Width="100"
         Height="30">

    <Control.Tag>
        <x:Static Member="local:ToothVisualModelType.Tooth" />
    </Control.Tag>
</Control>

Or you can create a static class like so:

或者你可以创建一个静态类,比如:

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

In XAML also used like this:

在XAML中也有这样的用法:

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

#2


2  

I found the solution after reading this. I thought this was done automatically or internally, but the solution is that I have to explicitly declare the local namespace in the Window tag like so:

我读了这篇文章后找到了解决办法。我认为这是自动完成的或者是内部完成的,但是解决方案是我必须像这样明确地声明窗口标记中的本地名称空间:

xmlns:local="clr-namespace:Dental_Viewer"

Then <xctk:ColorPicker Tag="{x:Static local:ToothVisualModelType.Tooth}"/> works like a charm.

然后< xctk:选择器标签= " { x:静态局部:ToothVisualModelType。“/>的工作就像一个咒语。”

#1


3  

Try to use this expression:

试着用这个表达:

<Control Name="MyControl"
         Width="100"
         Height="30">

    <Control.Tag>
        <x:Static Member="local:ToothVisualModelType.Tooth" />
    </Control.Tag>
</Control>

Or you can create a static class like so:

或者你可以创建一个静态类,比如:

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

In XAML also used like this:

在XAML中也有这样的用法:

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

#2


2  

I found the solution after reading this. I thought this was done automatically or internally, but the solution is that I have to explicitly declare the local namespace in the Window tag like so:

我读了这篇文章后找到了解决办法。我认为这是自动完成的或者是内部完成的,但是解决方案是我必须像这样明确地声明窗口标记中的本地名称空间:

xmlns:local="clr-namespace:Dental_Viewer"

Then <xctk:ColorPicker Tag="{x:Static local:ToothVisualModelType.Tooth}"/> works like a charm.

然后< xctk:选择器标签= " { x:静态局部:ToothVisualModelType。“/>的工作就像一个咒语。”