如何在XAML中使用枚举类型?

时间:2023-01-26 13:14:41

I'm learning WPF and I encountered the following problem:

我正在学习WPF,遇到了以下问题:

I have an enum type in another namespace than my XAML:

我在另一个名称空间中有一个enum类型,而不是我的XAML:

 public enum NodeType
    {
        Type_SYSTEM = 1,              // System
        Type_DB = 2,                  // Database
        Type_ROOT = 512,              // Root folder
        Type_FOLDER = 1024,           // Folder
    }

in my XAML I'd like to trigger an image with an integer

在我的XAML中,我想要触发一个带有整数的图像。

<Image.Style>
    <Style TargetType="{x:Type Image}">
        <Style.Triggers>
            <DataTrigger Binding="{Binding Type}" Value="{NodeType: }">
                <Setter Property="Source" Value="/Images/DB.PNG"/>
            </DataTrigger>
            <DataTrigger Binding="{Binding Type}" Value="128">
                <Setter Property="Source" Value="/Images/SERVER.PNG"/>
            </DataTrigger>
        </Style.Triggers>
    </Style>
</Image.Style>

Is there a way to get an integer value and compare it with an enum type directly in XAML code?

是否有一种方法可以直接在XAML代码中获取整数值并将其与enum类型进行比较?

My enum is in namespace AnotherNamespace.Types

我的enum位于名称空间AnotherNamespace.Types中

<DataTrigger Binding="{Binding IntegerType}" Value="MyEnumType.Type_DB">
                                        <Setter Property="Source" Value="/Images/SERVER.PNG"/> 

2 个解决方案

#1


41  

I had a similar question here, and my end result was to create a generic IValueConverter that passed the enum value I wanted to match in as the ConverterParameter, and it returns true or false depending on if the bound value matches the (int) value of the Enum.

我在这里有一个类似的问题,我的最终结果是创建一个通用的IValueConverter,它传递了我想作为ConverterParameter匹配的enum值,它返回true或false,具体取决于绑定值是否匹配enum的(int)值。

The end result looks like this:

最终结果如下:

XAML Code:

XAML代码:

<DataTrigger Value="True"
             Binding="{Binding SomeIntValue, 
                 Converter={StaticResource IsIntEqualEnumConverter},
                 ConverterParameter={x:Static local:NodeType.Type_DB}}">

Converter

转换器

public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
    if (parameter == null || value == null) return false;

    if (parameter.GetType().IsEnum && value is int)
    {
        return (int)parameter == (int)value;
    } 
    return false;
}

#2


9  

You just need to make sure that your namespace is accounted-for in your XAML header then you can reference both custom DPs and enum values directly in your markup.

您只需要确保您的名称空间在XAML头中是记帐的,然后您就可以直接在标记中引用自定义的DPs和enum值。

For example I use this code to do just that:

例如,我用这个代码来做:

<DataTemplate.Triggers>
  <MultiDataTrigger>
    <MultiDataTrigger.Conditions>
      <Condition Binding="{Binding IsMouseOver, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBoxItem}}}" Value="True" />
      <Condition Binding="{Binding Type}" Value="{x:Static loc:AppProfileItemType.Custom}" />
    </MultiDataTrigger.Conditions>
    <MultiDataTrigger.Setters>
      <Setter TargetName="PART_Delete" Property="Visibility" Value="{x:Static Visibility.Visible}" />
    </MultiDataTrigger.Setters>
  </MultiDataTrigger>
</DataTemplate.Triggers>

Note that you can't access DataTriggers in a Style, you need to instead make a DataTemplate or ControlTemplate for that (however, .NET 4 adds the ability to set triggers in styles). You can override the ControlTemplate from a Style like so:

注意,您不能以一种样式访问数据访问程序,您需要为此创建一个DataTemplate或ControlTemplate(然而,. net 4增加了在样式中设置触发器的能力)。您可以从以下样式重写ControlTemplate:

<Style x:Key="MyCustomButtonStyle" TargetType="Button">
  <Setter Property="Template">
    <Setter.Value>
      <ControlTemplate TargetType="Button">
        <ContentPresenter />
        <ControlTemplate.Triggers>
          <!-- Put your DataTriggers here -->
        </ControlTemplate.Triggers>
      </ControlTemplate>
    </Setter.Value>
  </Setter>
</Style>

For DataTemplates you want to have bindings to an object, you can simply use a ContentPresenter and set its content to the object you want to display along with a DataTemplate definition to use for display of the object. There's always a way to use DataTriggers it's just not always direct or as simple as using a Style.

对于想要对对象进行绑定的DataTemplate,您可以简单地使用ContentPresenter并将其内容设置为要显示的对象,以及用于显示对象的DataTemplate定义。总有一种使用DataTriggers的方式,只是不总是直接的,也不像使用样式那么简单。

#1


41  

I had a similar question here, and my end result was to create a generic IValueConverter that passed the enum value I wanted to match in as the ConverterParameter, and it returns true or false depending on if the bound value matches the (int) value of the Enum.

我在这里有一个类似的问题,我的最终结果是创建一个通用的IValueConverter,它传递了我想作为ConverterParameter匹配的enum值,它返回true或false,具体取决于绑定值是否匹配enum的(int)值。

The end result looks like this:

最终结果如下:

XAML Code:

XAML代码:

<DataTrigger Value="True"
             Binding="{Binding SomeIntValue, 
                 Converter={StaticResource IsIntEqualEnumConverter},
                 ConverterParameter={x:Static local:NodeType.Type_DB}}">

Converter

转换器

public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
    if (parameter == null || value == null) return false;

    if (parameter.GetType().IsEnum && value is int)
    {
        return (int)parameter == (int)value;
    } 
    return false;
}

#2


9  

You just need to make sure that your namespace is accounted-for in your XAML header then you can reference both custom DPs and enum values directly in your markup.

您只需要确保您的名称空间在XAML头中是记帐的,然后您就可以直接在标记中引用自定义的DPs和enum值。

For example I use this code to do just that:

例如,我用这个代码来做:

<DataTemplate.Triggers>
  <MultiDataTrigger>
    <MultiDataTrigger.Conditions>
      <Condition Binding="{Binding IsMouseOver, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBoxItem}}}" Value="True" />
      <Condition Binding="{Binding Type}" Value="{x:Static loc:AppProfileItemType.Custom}" />
    </MultiDataTrigger.Conditions>
    <MultiDataTrigger.Setters>
      <Setter TargetName="PART_Delete" Property="Visibility" Value="{x:Static Visibility.Visible}" />
    </MultiDataTrigger.Setters>
  </MultiDataTrigger>
</DataTemplate.Triggers>

Note that you can't access DataTriggers in a Style, you need to instead make a DataTemplate or ControlTemplate for that (however, .NET 4 adds the ability to set triggers in styles). You can override the ControlTemplate from a Style like so:

注意,您不能以一种样式访问数据访问程序,您需要为此创建一个DataTemplate或ControlTemplate(然而,. net 4增加了在样式中设置触发器的能力)。您可以从以下样式重写ControlTemplate:

<Style x:Key="MyCustomButtonStyle" TargetType="Button">
  <Setter Property="Template">
    <Setter.Value>
      <ControlTemplate TargetType="Button">
        <ContentPresenter />
        <ControlTemplate.Triggers>
          <!-- Put your DataTriggers here -->
        </ControlTemplate.Triggers>
      </ControlTemplate>
    </Setter.Value>
  </Setter>
</Style>

For DataTemplates you want to have bindings to an object, you can simply use a ContentPresenter and set its content to the object you want to display along with a DataTemplate definition to use for display of the object. There's always a way to use DataTriggers it's just not always direct or as simple as using a Style.

对于想要对对象进行绑定的DataTemplate,您可以简单地使用ContentPresenter并将其内容设置为要显示的对象,以及用于显示对象的DataTemplate定义。总有一种使用DataTriggers的方式,只是不总是直接的,也不像使用样式那么简单。