Dynamically changing the styles of controls - c#

I have created a screen (Screen1.xaml) that has few text boxes and dropdowns. Since the properties of all the text boxes are same, I have created a styles file(stylesheet.xaml) with the properties like width, height, Font size etc. like this
<Style x:Key="TextBox.Base" TargetType="Control" BasedOn="{StaticResource TextBlock.Base}">
<Setter Property="Width" Value="250"/>
<Setter Property="FontSize" Value="10"/>
<Setter Property="Height" Value="15"/>
<Setter Property="FontFamily" Value="Arial"/>
</Style>
Now, I want to dynamically change the properties of the controls depending on some conditions. I want to acheive this doing something in the code-behind. Please help.

You can pick a style and change it in code-behind. Depending on the way you have included it to your project there are several ways:
// the style defined in the app.xaml (you need a key)
Style globalStyle = Application.Current.Resources["Key"] as Style;
// the style defined for a control (you need its key)
UserControl control = ...
Style controlStyle = control.Resources["Key"] as Style;
// the current style of the control
Style currentStyle = control.Style;
Change the style like this:
style.SetValue(UserControl.FontSizeProperty, (float)10);
Edit:
As I read now, to change a style will only affect all controls that use it in WPF. In Silverlight, you might change the property for all controls :(

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.

How to set ProgressBarForeground in MahApps.Metro ProgressDialog?

I want to change the color of the progressbar inside of an ProgressDialog to HighlightColor.
With a CustomResourceDictionary i managed to change the button-style but not the color of the Progressbar
MetroDialogOptions.CustomResourceDictionary = new ResourceDictionary
{
Source = new Uri("pack://application:,,,/GUI;component/Styles/DialogDictionary.xaml")
};
DialogDictionary.xaml:
<Style x:Key="AccentedDialogSquareButton"
BasedOn="{StaticResource HighlightedSquareButtonStyle}"
TargetType="{x:Type ButtonBase}">
<Setter Property="Controls:ControlsHelper.ContentCharacterCasing" Value="Normal" />
</Style>
<Style TargetType="{x:Type Dialog:ProgressDialog}"
x:Key="NewProgressDialogStyle"
BasedOn="{StaticResource {x:Type Dialog:ProgressDialog}}">
<Setter Property="ProgressBarForeground" Value="Red" />
</Style>
<Style TargetType="{x:Type Dialog:ProgressDialog}"
BasedOn="{StaticResource NewProgressDialogStyle}" />
This is currently not possible.
The ProgressBarForeground property is set to a local value using the SetResourceReference method, so using a Setter in a Style has no effect - the local value always has a higher priority than the one coming from a Style.
You could try overriding the AccentColorBrush resource in your custom dictionary, but that won't work because the current theme code in Mahapps will scan all the merged resource dictionaries of the dialog and replace this resource with a special one that is defined as the "theme accent color".
See ThemeManager source code for details.
You have only two options:
change the accent color of your theme to the desired progress bar color (but then many controls will use this color for their appearance)
post a change request to the Mahapps team so that they provide a possibility to override the progress bar color in the ProgressDialog

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 can I override BorderBrush?

I have a style to a textbox, and I want in the application layer override de BorderBrush of this style.
I tried: d:LayoutOverrides="BorderBrush". But this doesn't work.
I want the same style, but with a red BorderBrush.
How can I do this please?
Thank you.
as you mentioned Blend in your tags: you can right-click a textbox search templates and create new ones from the existing (Edit a Copy). This will extract the complete definition of the textbox and you can change everything you want there.
Here is everything explained step-by-step: Create or edit a control template
Base your style on your old one and change the Border brush
<Style TargetType="TextBox" BasedOn="{StaticResource oldBrushKey}">
<Setter Property="BorderBrush" Value="Red" />
</Style>
or if it's the default style you want to override use
BasedOn="{StaticResource {x:Type TextBox}}"

Categories

Resources