C
C#2y ago
yotabit

Style dependent on a property

A Button ConfirmationButton should have the Style= MahApps.Styles.Button.Dialogs.Accent when the boolean property CancellationVisible that I'm binding is True. The code below shows how I tried to do it, but the problem is that it shows this error: error MC3029: 'Style' member is not valid because it does not have a qualifying type name. How can I fix it or achieve the same behavior?
<Button x:Name="ConfirmationButton" >
<Button.Triggers>
<DataTrigger Binding="{Binding CancellationVisible}" Value="True">
<Setter Property="Style" Value="{StaticResource MahApps.Styles.Button.Dialogs.Accent}" />
</DataTrigger>
</Button.Triggers>
</Button>
<Button x:Name="ConfirmationButton" >
<Button.Triggers>
<DataTrigger Binding="{Binding CancellationVisible}" Value="True">
<Setter Property="Style" Value="{StaticResource MahApps.Styles.Button.Dialogs.Accent}" />
</DataTrigger>
</Button.Triggers>
</Button>
9 Replies
SuperBrain
SuperBrain2y ago
You're not using Styles properly. See this page for clarification https://wpf-tutorial.com/styles/trigger-datatrigger-event-trigger/
yotabit
yotabit2y ago
I first tried something similar to what is written there, but it gives an error that I can't use a Style to change its Style
<Button x:Name="ConfirmationButton">
<Button.Style>
<Style TargetType="Button">
<Style.Triggers>
<DataTrigger Binding="{Binding CancellationVisible}" Value="False">
<Setter Property="Style" Value="{StaticResource MahApps.Styles.Button.Dialogs.Accent}" />
</DataTrigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
<Button x:Name="ConfirmationButton">
<Button.Style>
<Style TargetType="Button">
<Style.Triggers>
<DataTrigger Binding="{Binding CancellationVisible}" Value="False">
<Setter Property="Style" Value="{StaticResource MahApps.Styles.Button.Dialogs.Accent}" />
</DataTrigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
Any ideas how I could fix it? As far as I understood from it, that's indeed not the answer. I'm still looking for how I can do it. Do you have more suggestions?
Korbah
Korbah2y ago
I'm fairly certainly you cannot change a style from within a style. However, I believe you're trying to replicate the functionality of the Accented Button, which is just the standard button with the background replaced MahApps.Brushes.Accent You can do that by replacing your Setter Property to Backgound and your Value to {DynamicResource MahApps.Brushes.Accent} the only reason I know is because I struggled to integrate some of the MahApps theming in some custom controls (scatterplots to be specific), and eventually found I could use the dynamic resource brushes to get the theme colors
yotabit
yotabit2y ago
Thank you for you comment! The problem is: the Accent Button is not just the Default button with a different background(Accent). When {StaticResource MahApps.Styles.Button.Dialogs.Accent} style is applied, it also changed properties like the Foreground to white, on mouse over, etc Here's the code that changes the background:
<Button.Style>
<Style TargetType="Button">
<Style.Triggers>
<DataTrigger Binding="{Binding CancellationVisible}" Value="False">
<Setter Property="Background" Value="{DynamicResource MahApps.Brushes.Accent}" />
</DataTrigger>
</Style.Triggers>
</Style>
</Button.Style>
<Button.Style>
<Style TargetType="Button">
<Style.Triggers>
<DataTrigger Binding="{Binding CancellationVisible}" Value="False">
<Setter Property="Background" Value="{DynamicResource MahApps.Brushes.Accent}" />
</DataTrigger>
</Style.Triggers>
</Style>
</Button.Style>
yotabit
yotabit2y ago
This is how the button looks like
yotabit
yotabit2y ago
This here is what I want - (it's not only the foreground that is different)
yotabit
yotabit2y ago
I would prefer to not set every property, but just use the style that is ready I found all properties that are changed when accent is applied https://github.com/MahApps/MahApps.Metro/blob/8d46a8a8447085887703f0ab0ecae4ad95ec9289/src/MahApps.Metro/Styles/Controls.Buttons.xaml#L594
Korbah
Korbah2y ago
I'm not finding a great way to change a control's style based on a binding. You can absolutely change a control's style from the code behind. Maybe someone has a better idea, but you could have 2 different controls with different styles and change their visibility based on the binding
yotabit
yotabit2y ago
Yes. I didn't find that either. But the codebehind solution or their visibility works as well, so I'll use that. Thank you again!