在WPF中如何在枚举的情况下定义数据模板?

时间:2021-03-29 16:12:46

I have a Enum defined as Type

我将Enum定义为Type

public Enum **Type**
{
   OneType,
   TwoType,
   ThreeType
};

Now I bind Type to a drop down Ribbon Control Drop Down Menu in a Ribbon Control that displays each menu with a MenuName with corresponding Image.

现在,我将Type绑定到Ribbon控件中的下拉功能区控件下拉菜单,该控件显示带有MenuName和相应Image的每个菜单。

( I am using Syncfusion Ribbon Control ).

(我正在使用Syncfusion Ribbon Control)。

I want that each enum type like ( OneType ) has data template defined that has Name of the menu and corrospending image.

我希望像(OneType)这样的每个枚举类型都定义了具有菜单名称和腐蚀图像的数据模板。

How can I define the data template of enum ?

如何定义枚举的数据模板?

Please suggest me the solution, if this is possible !!

如果可能,请建议我的解决方案!

Please also tell me if its not possible or I am thinking in the wrong direction !!

请告诉我,如果不可能,或者我在想错误的方向!

3 个解决方案

#1


12  

One way to do it would be to create a DataTemplateSelector, and assign it to the ItemTemplateSelector property of the menu. In the code of the DataTemplateSelector, you just need to return a DataTemplate based on the enum value

一种方法是创建一个DataTemplateSelector,并将其分配给菜单的ItemTemplateSelector属性。在DataTemplateSelector的代码中,您只需要根据枚举值返回DataTemplate

#2


10  

Not sure whether this is an applicable solution to your particular situation, but it is relevant to the question of DataTemplate for enum. It is possible to create one DataTemplate for the enum type and use DataTriggers to tweak the controls in that template for individual enum value:

不确定这是否适用于您的特定情况,但它与DataTemplate for enum的问题相关。可以为枚举类型创建一个DataTemplate,并使用DataTriggers调整该模板中各个枚举值的控件:

enum MyEnumType {
    ValueOne,
    ValueTwo,
}

<DataTemplate DataType="{x:Type MyEnumType}">
    <TextBlock x:Name="valueText"/>
    <DataTemplate.Triggers>
        <DataTrigger Binding="{Binding}" Value="{x:Static MyEnumType.ValueOne}">
            <Setter TargetName="valueText" Property="Text" Value="First Value" />
        </DataTrigger>
    </DataTemplate.Triggers>
    <DataTemplate.Triggers>
        <DataTrigger Binding="{Binding}" Value="{x:Static MyEnumType.ValueTwo}">
            <Setter TargetName="valueText" Property="Text" Value="Second Value" />
        </DataTrigger>
    </DataTemplate.Triggers>
</DataTemplate>

#3


2  

It's very often the case that people use enums when they should be using polymorphism. You should, at the least, check to see if this is one of those cases. The presence of switch blocks in your class's code that check the value of the instance's enum is often a sign that this is a good idea. If you can eliminate the enum by defining subclasses, then you don't have to mess around with the likes of data template selectors and value converters.

人们经常使用枚举时应该使用多态。你应该至少检查一下这是否是其中一种情况。类的代码中存在切换块,用于检查实例枚举的值,这通常表明这是一个好主意。如果你可以通过定义子类来消除枚举,那么你就不必乱用数据模板选择器和值转换器。

#1


12  

One way to do it would be to create a DataTemplateSelector, and assign it to the ItemTemplateSelector property of the menu. In the code of the DataTemplateSelector, you just need to return a DataTemplate based on the enum value

一种方法是创建一个DataTemplateSelector,并将其分配给菜单的ItemTemplateSelector属性。在DataTemplateSelector的代码中,您只需要根据枚举值返回DataTemplate

#2


10  

Not sure whether this is an applicable solution to your particular situation, but it is relevant to the question of DataTemplate for enum. It is possible to create one DataTemplate for the enum type and use DataTriggers to tweak the controls in that template for individual enum value:

不确定这是否适用于您的特定情况,但它与DataTemplate for enum的问题相关。可以为枚举类型创建一个DataTemplate,并使用DataTriggers调整该模板中各个枚举值的控件:

enum MyEnumType {
    ValueOne,
    ValueTwo,
}

<DataTemplate DataType="{x:Type MyEnumType}">
    <TextBlock x:Name="valueText"/>
    <DataTemplate.Triggers>
        <DataTrigger Binding="{Binding}" Value="{x:Static MyEnumType.ValueOne}">
            <Setter TargetName="valueText" Property="Text" Value="First Value" />
        </DataTrigger>
    </DataTemplate.Triggers>
    <DataTemplate.Triggers>
        <DataTrigger Binding="{Binding}" Value="{x:Static MyEnumType.ValueTwo}">
            <Setter TargetName="valueText" Property="Text" Value="Second Value" />
        </DataTrigger>
    </DataTemplate.Triggers>
</DataTemplate>

#3


2  

It's very often the case that people use enums when they should be using polymorphism. You should, at the least, check to see if this is one of those cases. The presence of switch blocks in your class's code that check the value of the instance's enum is often a sign that this is a good idea. If you can eliminate the enum by defining subclasses, then you don't have to mess around with the likes of data template selectors and value converters.

人们经常使用枚举时应该使用多态。你应该至少检查一下这是否是其中一种情况。类的代码中存在切换块,用于检查实例枚举的值,这通常表明这是一个好主意。如果你可以通过定义子类来消除枚举,那么你就不必乱用数据模板选择器和值转换器。