Trouble with DataTriggers when the bindings rarely -- if ever -- change
Posted: 17 Aug 2022, 02:20
We are using an ObservableCollection that contains elements of type MyItem, where a MyItem contains a field called MyItemCategory that is an enum. In our xaml we have data triggers that look like this:
The category of each item can change at runtime, but in practice rarely does, which means the value of MyItemCategory isn't changing and therefore the above triggers aren't firing (I am assuming that the value is set before the view loads). We tried adding this trigger to account for that as a test:
With the above LoadedEventTrigger hack, the FIRST element of the list isn't properly transitioning into the MyItemCategoryA state, but all subsequent list elements are. Furthermore, there doesn't seem to be a way to switch states based on the bound value in MyItemCategory with this approach, so it doesn't really solve the problem.
Is there some other way we should go about doing this? Is there a bug here?
Code: Select all
<b:DataTrigger Binding="{Binding MyItemCategory}" Comparison="Equal" Value="{x:Static definitions:MyItemCategory.None}">
<b:GoToStateAction x:Name="MyItemCategoryNoneTrigger" StateName="MyItemCategoryNone" TargetName="Root"/>
</b:DataTrigger>
<b:DataTrigger Binding="{Binding MyItemCategory}" Comparison="Equal" Value="{x:Static definitions:MyItemCategory.CategoryA}">
<b:GoToStateAction x:Name="MyItemCategoryATrigger" StateName="MyItemCategoryA" TargetName="Root"/>
</b:DataTrigger>
<b:DataTrigger Binding="{Binding MyItemCategory}" Comparison="Equal" Value="{x:Static definitions:MyItemCategory.CategoryB}">
<b:GoToStateAction x:Name="MyItemCategoryBTrigger" StateName="MyItemCategoryB" TargetName="Root"/>
</b:DataTrigger>
<b:DataTrigger Binding="{Binding MyItemCategory}" Comparison="Equal" Value="{x:Static definitions:MyItemCategory.CategoryC}">
<b:GoToStateAction x:Name="MyItemCategoryCTrigger" StateName="MyItemCategoryC" TargetName="Root"/>
</b:DataTrigger>Code: Select all
<b:EventTrigger EventName="Loaded">
<b:GoToStateAction x:Name="LoadedEventTrigger" StateName="MyItemCategoryA" TargetName="Root"/>
</b:EventTrigger>Is there some other way we should go about doing this? Is there a bug here?