Using a DataTrigger with Multiple Values ​​in WPF

I am using the XAML code of which the

<DataTrigger Binding="{Binding Path=Name}" Value="1"> <Setter Property="Header" TargetName="expanderHeader" Value="Course Text"/> </DataTrigger> <DataTrigger Binding="{Binding Path=Name}" Value="2"> <Setter Property="Header" TargetName="expanderHeader" Value="Media Lecture"/> </DataTrigger> <DataTrigger Binding="{Binding Path=Name}" Value="3"> <Setter Property="Header" TargetName="expanderHeader" Value="Assessment"/> <DataTrigger Binding="{Binding Path=Name}" Value="4"> <Setter Property="Header" TargetName="expanderHeader" Value="Question"/> </DataTrigger> <DataTrigger Binding="{Binding Path=Name}" Value="5"> <Setter Property="Header" TargetName="expanderHeader" Value="General"/> </DataTrigger> 

Now I want to use dataTrigger, which can have values ​​like 3, 301, 302 or 303. How can I do this. I tried with Multidatatrigger, but it did not work. The code I tried with Multidatatrigger was:

 <MultiDataTrigger> <MultiDataTrigger.Conditions> <Condition Binding="{Binding Path=Name}" Value="301"/> <Condition Binding="{Binding Path=Name}" Value="301"/> <Condition Binding="{Binding Path=Name}" Value="302"/> <Condition Binding="{Binding Path=Name}" Value="303"/> </MultiDataTrigger.Conditions> <Setter Property="Header" TargetName="expanderHeader" Value="Assessment"/> </MultiDataTrigger> 
+6
source share
2 answers

MultiDataTrigger is applied when all conditions are not satisfied, so your MultiDataTrigger does not work.

Either have four separate triggers, or put the condition in a separate logical property, such as

 bool ShowAssessment { return Value == 301 || Value == 302 ... } 

raise a property change notification for ShowAssessment when the value changes

eg

 int Value { get{ return _value; } set { _value = value; RaisePropertyChanged("Value"); RaisePropertyChanged("ShowAssessment"); } } 

and then click on ShowAssessment.

Perhaps the best solution is to add a HeaderText property with a switch statement that returns the correct header text based on Value . Then just bind the title text to this. Triggers are not required.

+9
source

MultidataTrigger will act when all conditions are met. So

 <MultiDataTrigger> <MultiDataTrigger.Conditions> <Condition Binding="{Binding Path=Prop1}" Value="301"/> <Condition Binding="{Binding Path=Prop2}" Value="301"/> <Condition Binding="{Binding Path=Prop3}" Value="302"/> <Condition Binding="{Binding Path=Prop4}" Value="303"/> </MultiDataTrigger.Conditions> <Setter Property="Header" TargetName="expanderHeader" Value="Assessment"/> </MultiDataTrigger> 

You will need one DataTrigger

 <DataTrigger Binding="{Binding Path=ConditionEnum}" Value="1"> <Setter Property="Header" TargetName="expanderHeader" Value="Course Text"/> </DataTrigger> 

Set the Enum value with specific logic in your code ...

Example:

 Validate() { if(Value == 301 || Value == 302|| ....) { ConditionEnum = MyEnum.Assessment; } //Other Conditions } 

I leave the implementation of INotifyPropertyChanged . Use it to notify you of changes.

0
source

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


All Articles