Overriding default style for border makes my WPF window rounded - wpf

I override borders CornerRadius as a default style in the app.xaml file (like below)
<Application x:Class="BorderCornerProblem.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml">
<Application.Resources>
<Style TargetType="{x:Type Border}">
<Setter Property="OverridesDefaultStyle" Value="true"/>
<Setter Property="CornerRadius" Value="50"/>
</Style>
</Application.Resources>
</Application>
and in the MainWindow.xaml file I have
<Window x:Class="BorderCornerProblem.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
</Window>
the result is that the window has a black rounded corners.
My question then is how to define a DefaultStyle with CornerRadius set for Border that will not mess with my Window?

You really should not globally style Borders, they are everywhere.
Give the style a key and only reference it where needed.

Related

Style Defined in app.xaml is only applied in the Designer but not a Runtime

I put the two following Style in App.xaml of my WPF application. If I change the FontSize to a different value, the Designer of Visual Studio 2019 shows all the controls with the specified FontSize. If I run the app, the controls show a FontSize of 12.
<Application x:Class="testapp.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:testapp"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="View/Themes/ButtonStyle.xaml"/>
<ResourceDictionary Source="View/Themes/CheckBoxStyle.xaml"/>
<ResourceDictionary Source="View/Themes/ComboBoxStyle.xaml"/>
<ResourceDictionary Source="View/Themes/DataGridStyle.xaml"/>
</ResourceDictionary.MergedDictionaries>
<Style TargetType="{x:Type Page}">
<Setter Property="FontSize" Value="18" />
</Style>
<Style TargetType="{x:Type Window}">
<Setter Property="FontSize" Value="18" />
</Style>
</ResourceDictionary>
</Application.Resources>
If you research a bit, there has been an issue on this going back ten+ years on windows/pages. In design time the designer will get the style, but due to the Page/Window being derived types, during runtime they won't.
The fix (or workaround depending one one's viewpoint) is to name the style in app.xaml with x:key such as (page only shown for brevity):
<Style x:Key="pStyle" TargetType="{x:Type Page}">
<Setter Property="FontSize" Value="18" />
</Style>
and then set to the static resource style on each page/window such as below
Style="{StaticResource pStyle}"
such as:
<Page x:Class="WPFStack.ListBox.ListViewQuestion"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:WPFStack.ListBox"
mc:Ignorable="d"
xmlns:model="clr-namespace:WPFStack.Model"
d:DesignHeight="450" d:DesignWidth="800"
Style="{StaticResource pStyle}"
Title="ListViewQuestion">
See How to set default WPF Window Style in app.xaml

Setting WPF style on Window

I wrote an XAML code in wpf, i defined a style in window.resourse like this
<Window x:Class="WpfApp1.Test"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:self="clr-namespace:WpfApp1"
xmlns:system="clr-namespace:System;assembly=mscorlib"
xmlns:local ="clr-namespace:WpfApp1"
mc:Ignorable="d"
Height="400 " Width="450" >
<Window.Resources>
<Style TargetType="{x:Type Window}">
<Setter Property="Title" Value="Hello my friens!"/>
</Style>
</Window.Resources>
<Grid>
</Grid>
in here, Test is my window class name. when i run that everything is ok but when i changed above to this
<Window.Resources>
<Style TargetType="{x:Type Window}">
<Setter Property="Title" Value="Hello my friends!"/>
</Style>
</Window.Resources>
in design window, title showed as Value="Hello my friends!" but when i run the application, title become empty.
what this happens?
what is different btw TargetType="{x:Type Window}" and TargetType="{x:Type local:Test}" ?
did not every of them refer to window type ?
By just specifying the targettype in a style, the style automatically applies to all objects of the type you defined it for. However, that does not work for base classes.
In your example, TargetType="{x:Type Window}" will automatically apply the title "Hello my friends!" to all windows. However, the type of your window is not Window, but WpfApp1.Test. Window is just the base class that is used. Thats why the style doesn't apply to the window automatically.
If you use TargetType="{x:Type local:Test}" instead, it will apply automatically to all objects that have the type WpfApp1.Test, which is true for your window. The automatic applying of styles only works for the specific type, not the base class.
You can also specify a key attribute, and then tell your window that it should use this style. In this case, you can also use x:Type Window, because then the style is being applied explicitly.
e.g.:
<Window x:Class="WpfApp1.Test"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:self="clr-namespace:WpfApp1"
xmlns:system="clr-namespace:System;assembly=mscorlib"
xmlns:local ="clr-namespace:WpfApp1"
mc:Ignorable="d"
Height="400 " Width="450" Style="{DynamicResource MyStyle}">
<Window.Resources>
<Style TargetType="{x:Type Window}" x:Key="MyStyle">
<Setter Property="Title" Value="Hello my friends!"/>
</Style>
</Window.Resources>

How to make Foreground color of all controls White by default

1- Create a new WPF application.
2- Add some controls to your WPF application.
3- See that Foreground color of all controls are Black by default.
How to make Foreground color of all controls White by default?
"Foreground color of all controls are Black by default." because it is Windows theme setting.
you can assign your own foreground brush using ControlTextBrushKey key
<Application.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.ControlTextBrushKey }" Color="Green"/>
</Application.Resources>
this setting will be immediately seen in designer in property grid
You could try to define an implicit TextBox style in your window's XAML or in App.xaml:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplication1"
mc:Ignorable="d"
Title="MainWindow" Height="300" Width="300">
<Window.Resources>
<Style TargetType="TextBlock">
<Setter Property="Foreground" Value="White" />
</Style>
</Window.Resources>
...
That's probably the closest you get to setting the text colour of all controls in one place.

Override Specific Style Properties using Resource Dictionary

I am trying to override the styles of Material Design for Xaml ToolKit as per my requirements, the following is the xaml in app.xaml which i came up with after reading about overriding on the github page of the library, but it seems to be not working and i am not getting why, as i have not much experience working in WPF applications, here is the code i tried:
<Color x:Key="DarkBlueColor">#00479D</Color>
<FontFamily x:Key="MicrosoftYaHei">Microsoft YaHei</FontFamily>
<SolidColorBrush x:Key="WindowBrush" Color="#00479D"/>
<Style x:Key="WindowStyle"
x:Name="WindowStyle"
BasedOn="{StaticResource MaterialDesignPaper}"
TargetType="{x:Type Window}">
<Setter Property="Background" Value="{DynamicResource WindowBrush}"></Setter>
</Style>
For the time being to get familiar i am only trying to change the background of the window, here is the code from MainWindow.xaml:
<Window x:Class="WPFApplication.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:XCMG.CarMan2"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525"
Style="{StaticResource WindowStyle}"
xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes">
<Grid>
</Grid>
</Window>
When i run the application after adding the above code, it throws an exception saying:
Unable to cast object of type 'System.Windows.Media.SolidColorBrush' to type 'System.Windows.Style'.
"MaterialDesignPaper" is a SolidColorBrush and you can't base a Window style on a Brush.
Remove the BasedOn attribute and the x:Name from your Style:
<Style x:Key="WindowStyle"
TargetType="{x:Type Window}">
<Setter Property="Background" Value="{DynamicResource WindowBrush}"></Setter>
</Style>
but i want override MaterialDesignBrush BackGround
Define a new Brush resource with the same key then:
<SolidColorBrush x:Key="MaterialDesignPaper" Color="#00479D"/>

Applying global styles to combobox

I'm trying to apply a global style to all ComboBoxes in my application. I'm doing this by defining a Style in my App.xaml file and specifying a TargetType, which should apply that style to all controls that are of the specified type. However, it appears that my style is not being applied at all.
Here's my code:
App.xaml
<Application x:Class="Test.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Test"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<Style TargetType="{x:Type ComboBox}">
<Setter Property="Background" Value="Red"></Setter>
</Style>
</ResourceDictionary>
</Application.Resources>
</Application>
MainWindow.xaml
<Window x:Class="Test.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Test"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<ComboBox Margin="173,130,186,166"></ComboBox>
</Grid>
</Window>
I do not have any code-behind at this point other than the default code that VS generates for WPF forms.
I expect this XAML code to change the background of any ComboBoxes in any window to red, without me needing to manually specify the style for each ComboBox. (I really don't want write it out manually for each ComboBox - my app will end up using many, many CBs and it would be a major pain - not to mention it looks cluttered.)
I tried to model my code after this question but did not get any results.
Try to avoid the nested ResourceDictionary in the App.Xaml.
Fix in this way:
<Application.Resources>
<Style TargetType="{x:Type ComboBox}">
<Setter Property="Background" Value="Red"></Setter>
</Style>
</Application.Resources>
I would suggest to create a folder in your solution and add in it a Xaml control : ResourceDictionary where you gonna define all your global styles you want to apply by default.
For example :
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style TargetType="{x:Type TextBox}">
<Setter Property="Height" Value="25"/>
<Setter Property="Background" Value="red"></Setter>
</Style>
</ResourceDictionary>
Now, you just need to put a reference in you App.Xaml like this :
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Views/Style/GlobalStyle.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
Hope it'll help you.
Have a good day.

Resources