How to change button style based on if else using DataTriggers in wpf mvvm

I want to change the button style based on the else condition when the wpf application loads. When loading an application using if, there will be one button style, and in another part - another. How to do this using Datatriggers or using the MVVM pattern.

Please offer?

thanks

+4
source share
3 answers

You should study the data patterns and the pattern selector. Here is a copied copy of the plugin example from my own code, it is not immediately applicable to buttons, but I think it should help you on your way.

The following is the application resource xaml file. I use it to decide which view to use for a ProjectViewModel based on a variable in the ViewModel:

<DataTemplate DataType="{x:Type viewmod:ProjectViewModel}"> <DataTemplate.Resources> <DataTemplate x:Key="ProjectEditViewTemplate"> <view:ProjectEditView/> </DataTemplate> <DataTemplate x:Key="ServiceSelectionViewTemplate"> <view:ServiceSelectionView/> </DataTemplate> </DataTemplate.Resources> <ContentControl Content="{Binding}" ContentTemplateSelector="{StaticResource ProjectViewModelTemplateSelector}" /> </DataTemplate> 

ProjectViewModelTemplateSelector is defined as follows:

  public class ProjectViewModelTemplateSelector : DataTemplateSelector { public override DataTemplate SelectTemplate(object item, DependencyObject container) { FrameworkElement element = container as FrameworkElement; if (element != null && item != null && item is ViewModel.ProjectViewModel) { if ((item as ViewModel.ProjectViewModel).EditMode) { return element.FindResource("ProjectEditViewTemplate") as DataTemplate; } else { return element.FindResource("ServiceSelectionViewTemplate") as DataTemplate; } } else return base.SelectTemplate(item, container); } } 

}

+2
source

You can use Style.Setters to set the default value. For other specific conditions, use Style.Triggers . It works as if still.

 <TextBlock.Style> <Style TargetType="TextBlock"> <Style.Triggers> <DataTrigger Binding="{Binding ElementName=EditorWindow, Path=Category}" Value="R"> <Setter Property="Visibility" Value="Visible"/> </DataTrigger> </Style.Triggers> <Style.Setters> <Setter Property="Visibility" Value="Collapsed"/> </Style.Setters> </Style> </TextBlock.Style> 
+6
source

Alternatively, if you want to use DataTriggers, you can use the following:

  <Button Command="{Binding SomeButtonCommand}" Content="Click Me!"> <Button.Style> <Style TargetType="{x:Type Button}"> <Style.Triggers> <DataTrigger Binding="{Binding Path=NormalButtonMode, Mode=OneWay}" Value="True"> <Setter Property="Content" Value="This Button is in Normal Mode" /> </DataTrigger> <DataTrigger Binding="{Binding Path=NormalButtonMode, Mode=OneWay}" Value="False"> <Setter Property="Content" Value="This Button is in the Other Mode" /> </DataTrigger> </Style.Triggers> </Style> </Button.Style> </Button> 

In this case, the ViewModel should set the boolean property to NormalButtonMode. In this example, I only set the Content property, but you can specify any number of Setters inside the DataTrigger. You can also put this style in the resource dictionary and simply link it for each button using StaticResource. Just make sure you open the NormalButtonMode property (or something else) for each ViewModel - maybe put it in a base class.

+2
source

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


All Articles