I want to change the style of the Min, Max and Close buttons for my WPF application.
I'm using Mahapps.Metro and I have successfully managed to achieve the result I want, but only with the obsolete WindowMinButtonStyle, WindowMaxButtonStyle and WindowCloseButtonStyle properties in the MetroWindow class. The obsolete message on for example the WindowMinButtonStyle property reads:
This property will be deleted in the next release. You should use LightMinButtonStyle or DarkMinButtonStyle in WindowButtonCommands to override the style.
The problem is that I can't figure out how specifically to do that. The MetroWindow class has a field called WindowButtonCommands, but it is internal, so that seems to be the wrong tree to bark up. I'm fairly new to WPF and there is no info on how to do this in the guides on their website, so I'm pretty lost. I'm hoping someone can provide me with a short code example to point me in the right direction.
EDIT - Here is XAML that produces the warning:
<controls:MetroWindow x:Class="Project.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="http://metro.mahapps.com/winfx/xaml/controls"
WindowMinButtonStyle="{DynamicResource DarkWindowButtonStyle}"
WindowMaxButtonStyle="{DynamicResource DarkWindowButtonStyle}"
WindowCloseButtonStyle="{DynamicResource DarkWindowCloseButtonStyle}">
<Grid>
</Grid>
</controls:MetroWindow>
I should also mention I'm using the new v1.2.0 of Mahapps.Metro, but I had the same issue with the previous version.
Mahapps.Metro source code that has the Obsolete attributes: https://github.com/MahApps/MahApps.Metro/blob/develop/MahApps.Metro/Controls/MetroWindow.cs#L88-L93
based on crumbl3d changes, a short how to...
There are now two styles (Light, Dark) which will be used based on OverrideDefaultWindowCommandsBrush property (available at MetroWindow) and it's lightness (default is the Light style).
So, put these at your App.xaml (or something else)
<Style x:Key="CustomLightMetroWindowButtonStyle" TargetType="{x:Type Button}" BasedOn="{StaticResource LightMetroWindowButtonStyle}">
<Setter Property="Foreground" Value="Chocolate" />
</Style>
<Style x:Key="CustomDarkMetroWindowButtonStyle" TargetType="{x:Type Button}" BasedOn="{StaticResource DarkMetroWindowButtonStyle}">
<Setter Property="Foreground" Value="Crimson" />
</Style>
<Style TargetType="{x:Type controls:WindowButtonCommands}" BasedOn="{StaticResource {x:Type controls:WindowButtonCommands}}">
<Setter Property="LightMinButtonStyle" Value="{StaticResource CustomLightMetroWindowButtonStyle}" />
<Setter Property="LightMaxButtonStyle" Value="{StaticResource CustomLightMetroWindowButtonStyle}" />
<Setter Property="LightCloseButtonStyle" Value="{StaticResource CustomLightMetroWindowButtonStyle}" />
<Setter Property="DarkMinButtonStyle" Value="{StaticResource CustomDarkMetroWindowButtonStyle}" />
<Setter Property="DarkMaxButtonStyle" Value="{StaticResource CustomDarkMetroWindowButtonStyle}" />
<Setter Property="DarkCloseButtonStyle" Value="{StaticResource CustomDarkMetroWindowButtonStyle}" />
</Style>
Edit
If you only want to use this at one window then you can create a style with a key and use it at this window like this:
<controls:MetroWindow.WindowButtonCommands>
<controls:WindowButtonCommands Style="{DynamicResource CustomWindowButtonCommandsStyleLocatedtInAppXaml}" />
</controls:MetroWindow.WindowButtonCommands>
The style located in App.xaml
<Style x:Key="CustomWindowButtonCommandsStyleLocatedtInAppXaml" TargetType="{x:Type controls:WindowButtonCommands}" BasedOn="{StaticResource {x:Type controls:WindowButtonCommands}}">
<Setter Property="LightMinButtonStyle" Value="{StaticResource CustomLightMetroWindowButtonStyle}" />
<Setter Property="LightMaxButtonStyle" Value="{StaticResource CustomLightMetroWindowButtonStyle}" />
<Setter Property="LightCloseButtonStyle" Value="{StaticResource CustomLightMetroWindowButtonStyle}" />
<Setter Property="DarkMinButtonStyle" Value="{StaticResource CustomDarkMetroWindowButtonStyle}" />
<Setter Property="DarkMaxButtonStyle" Value="{StaticResource CustomDarkMetroWindowButtonStyle}" />
<Setter Property="DarkCloseButtonStyle" Value="{StaticResource CustomDarkMetroWindowButtonStyle}" />
</Style>
Hope this helps.
Related
I have some styles and templates in App.Xaml so I can acces them thru multiple UserControls.
EDIT : This is within the app.Xaml:
One of the Styles are :
<Application x:Class="BaseRefence.generatingAnnotation.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:BaseRefence.generatingAnnotation"
xmlns:Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero2"
StartupUri="MainWindow.xaml">
<Application.Resources>
<Style x:Key="ComboBoxStyleRounded" TargetType="{x:Type ComboBox}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TextBox}">
<Grid>
<Border CornerRadius="25"
BorderThickness="1,1,2,2">
</Border>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
<Setter Property="FocusVisualStyle" Value="{StaticResource FocusVisual}"/>
<Setter Property="Background" Value="{StaticResource ComboBox.Static.Background}"/>
<Setter Property="BorderBrush" Value="#42536b"/>
<Setter Property="Foreground" Value="Black"/>
<Setter Property="BorderThickness" Value="1,1,2,2"/>
<Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto"/>
<Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/>
<Setter Property="Padding" Value="6,3,5,3"/>
<Setter Property="ScrollViewer.CanContentScroll" Value="true"/>
<Setter Property="ScrollViewer.PanningMode" Value="Both"/>
<Setter Property="Stylus.IsFlicksEnabled" Value="False"/>
<Setter Property="Template" Value="{StaticResource ComboBoxTemplate}"/>
<Style.Triggers>
<Trigger Property="IsEditable" Value="true">
<Setter Property="IsTabStop" Value="false"/>
<Setter Property="Padding" Value="2"/>
<Setter Property="Template" Value="{StaticResource ComboBoxEditableTemplate}"/>
</Trigger>
</Style.Triggers>
</Style>
</Application.Resources>
</Application>
And in de UserControl.Xaml I have:
<ComboBox ItemsSource="{Binding ViewFamilyTypesInProject}"
SelectedItem="{Binding SelectedViewFamilyType, Mode=TwoWay}"
Grid.Row="1"
Grid.Column="1"
Margin="10 5"
MaxHeight="40"
Style="{DynamicResource ComboBoxStyleRounded}">
Within the designer everything works great, it shows correctly and all.
However, whenever I build and run my code, it gives the message and it does not override the style but keeps the default style.
System.Windows.ResourceDictionary Warning: 9 : Resource not found; ResourceKey='ComboBoxStyleRounded'
My experience: I frequently have same issues (using VS 16.6.2, .NET Core 3.1 and WPF).
Simply close Visual Studio and restart it: 98% of the times error disappears (will reappear sometime in the future).
You have an invalid Style. It may compile, as the XAML is syntactically correct, but the semantics of the markup are wrong. Since you are referencing the Style using DynamicResource the error occurs at runtime. But I wonder why don't get a XAML Designer error.
You are setting the ComboBox.Template property twice. Moreover and most important, the first ControlTemplate at the top of the Style is targeting the wrong type TextBox:
<ControlTemplate TargetType="{x:Type TextBox}">
<Grid>
<Border CornerRadius="25"
BorderThickness="1,1,2,2">
</Border>
</Grid>
</ControlTemplate>
The type must be of course <ControlTemplate TargetType="ComboBox">.
Since you are referencing a ControlTemplate resource later
<Setter Property="Template" Value="{StaticResource ComboBoxTemplate}" />
I think you want to remove the first ControlTemplate. If you want to change the appearance of the TextBox, you would need to override the complete ControlTemplate of the ComboBox.
You may should run Clean Solution and Rebuild Solution.
Let say I want to style a grid background like that :
<Page.Resources>
<Style x:key="MainGridStyle" TargetType="Grid">
<Setter Property="Background" Value="Blue" />
</Style>
</Page.Resources>
How can I make all TextBlocks inside that MainGridStyle Foreground=White through that Style?
Thanks
This works in WPF at least:
<Style x:key="MainGridStyle" TargetType="Grid">
<Setter Property="Background" Value="Blue" />
<Setter Property="TextElement.Foreground" Value="White" />
</Style>
I am working with the System.Windows.Ribbon in my project. I am also using some other libraries like AvalonDocking,... What I want to do is create my own themes inside the application so that the user can select the prefered theme.
The problem is that I don't get the RibbonTab to change to the correct colors. When I change the ribbon background color the RibbonTab color changes also. But I want to change it seperatly
Does anybody have any experience with changing the layout of the System.Windows.Ribbon?
This is what I tried before:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:xcad="http://schemas.xceed.com/wpf/xaml/avalondock">
<SolidColorBrush x:Key="WindowBrush" Color="Black"/>
<Style TargetType="{x:Type Ribbon}">
<Setter Property="Background" Value="#444444" />
<Setter Property="Foreground" Value="White" />
</Style>
<Style TargetType="{x:Type RibbonTab}">
<Setter Property="Height" Value="88" />
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
<Setter Property="Background" Value="Black" />
</Style>
<Style TargetType="{x:Type xcad:DockingManager}">
<Setter Property="Background" Value="#444444" />
<Setter Property="Foreground" Value="White" />
</Style>
</ResourceDictionary>
As you can see, the RibbonTab is not black like specified inside the ResourceDictionary
in WPF I override the basic control styles with the following Code:
<Style TargetType="{x:Type Button}" BasedOn="{StaticResource {x:Type Button}}">
<Setter Property="Padding" Value="5" />
<Setter Property="Margin" Value="{StaticResource margin}" />
</Style>
In UWP the x:Type in XAMl is gone. So how can I accomplish the above code in UWP? Thanks.
This should be it:
<Style TargetType="Button">
<Setter Property="Padding" Value="5" />
<Setter Property="Margin" Value="{StaticResource margin}" />
</Style>
From what I can gather from the documentation, x:Type is no longer supported in Universal Windows Apps.
To get around style inheritance, you will need to create a base style which has an x:Key, this can be referenced using the StaticResource markup for the BasedOn property.
<Style x:Key="BasicButtonStyle" TargetType="Button">
... (This may contain nothing)
</Style>
<Style TargetType="Button"
BasedOn="{StaticResource BasicButtonStyle}">
...
</Style>
In WPF i have a style for the control like below,
<Style TargetType="local:CustomControl">
<Setter Property="Background" Value="Transparent" />
<Setter Property="BorderBrush" Value="Gray" />
<Setter Property="BorderThickness" Value="0,0,0,1" />
<Setter Property="Padding" Value="3,0,3,0" />
<Setter Property="IsTabStop" Value="False" />
<Setter Property="VerticalContentAlignment" Value="Center" />
</Style>
Now i need to override customcontrol border for some other place like below,
<Style TargetType="local:CustomControl" BasedOn="{StaticResource {x:Type local:CustomControl}}">
<Setter Property="BorderThickness" Value="1" />
</Style>
My problem is when i am using above code it override the 1st written code. is my code is correct.
Note: the base style is only written with target type. i need to override that control border in some other place without affect base code.
is it possible ? please help me to resolve this problem.
thanks in advance.
If you declare a Style without an x:Key, it will override the default style for that control.
<Style TargetType="local:CustomControl">
So the code above will effect all CustomControl elements throughout the entire application (or within the scope).
If you do not want to override the base style, you can give your Style an x:Key, like so:
<Style TargetType="local:CustomControl" x:Key="MyAwesomeStyle">
When you create your control, you will then have to reference the Style. Here's an example:
<local:CustomControl Style="{DynamicResource MyAwesomeStyle}" ... />
Accidentally I saw some example which can be useful in solving the mentioned problem. In your example own custom control has been used, in my example - a button.
<Grid>
<Button Style="{StaticResource AddButtonStyle}" Tag="Add" Click="addClick" />
</Grid>
Code for AddButtonStyle:
<Style x:Key="AddButtonStyle" TargetType="Button" BasedOn="{StaticResource AppBarButtonStyle}">
<Setter Property="Content" Value="✅"/>
</Style>
AddButtonStyle based on AppBarButtonStyle. Below code for it.
<Style x:Key="AppBarButtonStyle" TargetType="Button">
<Setter Property="MinWidth" Value="40" />
<Setter Property="Width" Value="100" />
<Setter Property="Height" Value="88" />
<Setter Property="HorizontalAlignment" Value="Right" />
<Setter Property="Foreground" Value="White" />
<Setter Property="FontFamily" Value="Segoe UI Symbol" />
<Setter Property="FontSize" Value="18" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">. . .
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
On this example base, you must declare a Style with an x:Key, and should not set any value to Content (in you example BorderThickness) property in the inherited style.