How to set ProgressBarForeground in MahApps.Metro ProgressDialog? - c#

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

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.

Set a style color based on an object

I want to set the Background color of an object's style, to be the color of the Window Foreground. So.... how to get the color of one object and use it as the value in a style?
<Setter Property="Background" Value="????Window Foreground Color????" />
I've tried different binding combinations, but none of them have worked yet. In code-behind this value would be this.Foreground, but in XAML style?
Quickest way would be to give you parent Window a name and use that in binding. Something like this (if you named your window "Root"):
<Setter Property="Background" Value="{Binding ElementName=Root, Path=Foreground}" />

Dynamically changing the styles of controls

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 :(

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