Universal Windows applications do not support data triggers.
Without data triggers, how to change the background color of a button using xaml and data binding only when a boolean property in the view model changes?
For example, given this XAML:
<StackPanel> <Button Name="ButtonA" Click="ButtonA_Click" Content="A" /> <Button Name="ButtonB" Click="ButtonB_Click" Content="B" /> <Button Name="ButtonC" Click="ButtonC_Click" Content="C" /> </StackPanel>
with this code for
private void ButtonA_Click(object sender, RoutedEventArgs e) { Model.IsOnA = !Model.IsOnA; } private void ButtonB_Click(object sender, RoutedEventArgs e) { Model.IsOnB = !Model.IsOnB; } private void ButtonC_Click(object sender, RoutedEventArgs e) { Model.IsOnC = !Model.IsOnC; }
What is the best approach for changing the background color of buttons using data binding when changing the corresponding property in the view model?
I managed to get it to work with one button only with the help of the VisualStateManager manager:
<VisualStateManager.VisualStateGroups> <VisualStateGroup> <VisualState> <VisualState.StateTriggers> <StateTrigger IsActive="{x:Bind Model.IsOnA, Mode=OneWay}" /> </VisualState.StateTriggers> <VisualState.Setters> <Setter Target="ButtonA.Background" Value="Red"></Setter> <Setter Target="ButtonA.Foreground" Value="White"></Setter> </VisualState.Setters> </VisualState> </VisualStateGroup> </VisualStateManager.VisualStateGroups>
But with a few buttons that are associated with different properties in the view model, this approach does not work.
source share