In WPF, how to define a data template in case of enumeration?

I have an Enum defined as Type

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

Now I bind Type to the ribbon tag control drop-down menu in the ribbon control, which displays each menu using MenuName with the corresponding image.

(I am using Syncfusion Ribbon Control).

I want each type of enumeration of type OneType to have a specific data template that has a menu name and a corroding image.

How can I define an enumeration data template?

Please offer me a solution if possible!

Please tell me if this is not possible or I think in the wrong direction!

+4
source share
3 answers

One way to do this is to create a DataTemplateSelector and set its ItemTemplateSelector menu property. In the DataTemplateSelector code, you just need to return a DataTemplate based on the enumeration value

+13
source

Not sure if this solution is suitable for your specific situation, but it refers to the DataTemplate issue for enum. You can create one DataTemplate for the enum type and use DataTriggers to configure the controls in this template for a single enum value:

Enum:

 enum MyEnumType { ValueOne, ValueTwo, } 

Template:

 <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> <DataTrigger Binding="{Binding}" Value="{x:Static MyEnumType.ValueTwo}"> <Setter TargetName="valueText" Property="Text" Value="Second Value" /> </DataTrigger> </DataTemplate.Triggers> </DataTemplate> 
+13
source

It often happens that people use enumerations when they need to use polymorphism. You should at least check if this is one of these cases. Having switch blocks in your class code that check the value of an instance enumeration is often a sign that this is a good idea. If you can eliminate the enumeration by defining subclasses, then you do not need to bother with such selectors and value converters.

+2
source

Source: https://habr.com/ru/post/1304057/


All Articles