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?
9 Replies
You're not using Styles properly. See this page for clarification
https://wpf-tutorial.com/styles/trigger-datatrigger-event-trigger/
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
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?
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 colorsThank 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:
This is how the button looks like
This here is what I want - (it's not only the foreground that is different)
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
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
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!