Edit Button Visual State without Blend - c#

One can edit Foreground and Background etc... for a Button's default state without Blend using just the properties pane or code, but it is it possible to edit the colors for the other states without Blend?
For example, all I want is a button to turn gray on "Mouse Over". Anything I have seen on Stack Overflow or on the Intertubes uses Blend. I want to do it without it. Is it possible?

An easy way to do this is using Style Triggers.
<Style x:Key="HoverButtonStyle" TargetType="{x:Type Button}">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="Gray"/>
</Trigger>
</Style.Triggers>
</Style>
The style is defined in your Resources. And then when you define your button in XAML, you assign the style, like this:
<Button x:Name="MyButton" Style="{DynamicResource HoverButtonStyle}"/>
This style will change the Background brush of your Button when the mouse is over it. There are many more advanced things you can do with triggers, but this is a very simple example. If you google WPF Style Triggers, you'll find many examples, including this one which is pretty thorough.
There are other ways to do this, for example using the VisualStateManager, as described here, however if you're just trying to change the Background on hover, a style trigger is probably the simplest way.

Related

How to apply style in WPF to all controls without overriding their original styles?

I want to create a style, which will normalize margins for all controls in a specific scenario:
<Style TargetType="FrameworkElement" x:Key="MyStyle">
<Setter Property="Margin" Value="{StaticResource DialogItemsExceptTopMargin}" />
</Style>
I have then a couple of different controls: textboxes, comboboxes, checkboxes etc., to which I want to apply this style.
However, when I do that, their look is immediately reverted to the platform style (I am applying a style from 3rd party library). How can I define my style so that the original styles are kept intact?
Mind: I know, that I can use BasedOn for specific type:
<Style TargetType="TextBox" BasedOn="{StaticResource {x:Type TextBox}}">
...
</Style>
This however would require me to create completely separate styles for all possible controls, which I may use. I tried this trick with FrameworkElement and Control, but I got an error, that there is no such StaticResource.

WPF - Turn on/off keyboard from code behind

There's a custom touchscreen keyboard in my app built according to this: https://www.codeproject.com/Articles/32568/A-Touch-Screen-Keyboard-Control-in-WPF.
I also have a ResourceDictionary containing all styles and templates. In the TextBox style, I can set the keyboard ON/OFF:
<Style TargetType="{x:Type TextBox}">
<Setter Property="FontSize" Value="14" />
<Setter Property="Padding" Value="4" />
<Setter Property="k:TouchScreenKeyboard.TouchScreenKeyboard" Value="True"/>
</Style>
I would like the user to be able to turn it on or off from the UI, but can't figure out how to reach this property from code behind. I would like to make it without naming the style, since it's pretty commonly used throughout the app.
I tried this, but (no surprise) get ArgumentNotFoundException:
Style s = Application.Current.FindResource("defTextBox") as Style;
s.RegisterName("Keyboard.TouchScreenKeyboard.TouchScreenKeyBoard",false);
Any help would be appreciated!
You shoud be able to set the TouchScreenKeyboard attached property for an individual TextBox like this:
TouchScreenKeyboard.SetTouchScreenKeyboard(textBox1, false);
Changing the defintion of the implicit Style itself after it has already been applied to all TextBox elements doesn't make much sense though. You should define the default value in XAML and then change the value for individual TextBoxes dynamically at runtime if you need to.

MenuItem Style seems to be overwritten

It seems that the menuitem style that I am attempting to use gets totally overwritten when I use ItemContainerStyle.
Here's an example of what happens when I use it:
However, when I don't use it, this is what I get:
I much prefer the look of the second menu, but it doesn't support dynamic menu creation due to not using ItemContainerStyle. What could possibly be overwriting the style? I'm using Mahapps Dark base and VS colors/styles.
Base your custom Style on the MetroMenuItem style that comes with MahApps:
<Style TargetType="MenuItem" BasedOn="{StaticResource MetroMenuItem}">
<Setter Property="Background" Value="Yellow" />
</Style>
you should use BaseOn property in the ItemContainerStyle.
<ItemContainerStyle x:Key="MyContainerStyle" BaseOn="{DynamicResource MenuItemStyle}">Style here</ItemContainerStyle>

WPF DataGrid Header styling

I want to create a custom styled header for WPF DataGrid, but I don't know if it is possible to do what I want, and if it is how exactly should I do.
The sketch of the DataGrid is on the linked picture. The Purple header extends beyond the edge of the DataGrid and it has a little 3D bending.
DataGrid
Can I do something like this with WPF DataGrind and if yes how do I start?
Thanks!
DataGrid allows for ColumnHeaderStyle, CellStyle and many more styles to bet set. Right click DataGrid control in designer view > View Additional Templates will show you the complete list.
You have to use the following :
<Style x:Key="DataGridColumnHeaderStyle1" TargetType="{x:Type DataGridColumnHeader}">
<Setter Property="Background" Value="Orange"/>
<Setter Property="FontSize" Value="20"/>
<Setter Property="Padding" Value="5"/>
<Setter Property="BorderBrush" Value="Red"/>
<Setter Property="BorderThickness" Value="0 0 3 0"/>
</Style>
This will change the column headers. And if you want to change cells too, you can provide styles for DataGridCell too.
Use Snoop tool to peek inside a DataGrid first and peek into it to see what it looks like at runtime. This will clear many of your concepts.
It is possible, but it is a lot of work and requires very good knowledge of writing WPF templates. What you essentially want to do is replace the templates for DataGrid, and all other DataGrid related conotrols such as the DataGridRow, etc
Microsoft provide a full example of how to do this here: https://msdn.microsoft.com/en-us/library/vstudio/ff506248(v=vs.100).aspx

How to add a trigger to a WPF custom control without overriding the existing style?

I am creating a simple custom control extending from toggle button that allows the user to specify checked and unchecked content directly in XAML. It works well but it is based on a trigger, and I don't know how to define the trigger except in a style. If I define the style, then I lose anything set outside of the custom control.
What I would like to be able to do is just append this trigger to any existing style set elsewhere on the control.
Here's the XAML for the style/trigger.
<ToggleButton.Style>
<Style TargetType="{x:Type ToggleButton}" BasedOn="{StaticResource {x:Type ToggleButton}}">
<Setter Property="Content" Value="{Binding RelativeSource={RelativeSource Self}, Path=UncheckedContent}" />
<Style.Triggers>
<Trigger Property="IsChecked" Value="True">
<Setter Property="Content"
Value="{Binding RelativeSource={RelativeSource Self}, Path=CheckedContent}" />
</Trigger>
</Style.Triggers>
</Style>
</ToggleButton.Style>
I tried inheriting the style via the BasedOn with a default type but it won't work if the custom control has an explicit style set by its parent. I also considered EventTriggers but I do not believe there would be an event to initialize the control.
Thanks for any help anyone can offer. :)
Just to clear things up on the terminology here: A user control is a control that derives from the UserControl class. If I understood you right you derived from ToggleButton to add the UncheckedContent and CheckedContent properties. In that case you have created a custom control. It's always easier to follow if we agree on common terminology :)
As far as I know you can not do such a generic style inheritance in XAML. You always have to specify explicitly what style a another style is based upon. Your style can either be based on the default style for ToggleButton or on a specific other style. If you can't build a style inheritance chain that respects that, this approach won't work.
But since you have a custom control, couldn't you write a default style for it that is based on the default toggle button style like this?
<Style TargetType="{x:Type CustomToggleButton}"
BasedOn="{StaticResource {x:Type ToggleButton}}">
Then whenever you apply an explicit style to a toggle button you would specify that it is based on the default toggle button style.
Also you could write a (default) control template for your new toggle button in Themes\Generic.xaml that contains the above triggers. In blend you can get a copy of the default template for toggle button ("Edit Template"->"Edit a Copy") so you can make sure that your toggle button looks exactly like the normal toggle button. Then incorporate the triggers above into that template.
BTW: you do not have to create a new control just to add new properties. You can add new properties to an existing control using attached properties. They can be used from XAML just like normal properties.

Categories

Resources