using a Brush stored in a ResourceDictionary - wpf

I am a WPF newbie and I want to create a program wide Brush that I can reference again and again. I have a brush in a ResourceDicitonary with full definition.
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:PresentationOptions="http://schemas.microsoft.com/winfx/2006/xaml/presentation/options">
<DrawingBrush x:Key="rg1024_metal_effect" Stretch="Uniform">
I have added this to app.xaml.
<Color x:Key="SteelBrush">#FFFFFF</Color>
<SolidColorBrush x:Key="AppBrush" Color="{StaticResource rg1024_metal_effect}"/>
But I get the error:
"rg1024_metal_effect could not be resolved"
I know this should be easy but for a freshie like me - that's not so easy.

If you define a resource in a ResourceDictionary named for example "Dictionary1.xaml" and you want to reference this resource from another resource that you define in another ResourceDictionary, such as for example App.xaml, you should merge Dictionary1.xaml into App.xaml:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Dictionary1.xaml" />
</ResourceDictionary.MergedDictionaries>
<SolidColorBrush x:Key="AppBrush" Color="{StaticResource SteelBrush}"/>
</ResourceDictionary>
</Application.Resources>
Dictionary1.xaml:
<Color x:Key="SteelBrush">#FFFFFF</Color>
In your example, rg1024_metal_effect is not a Color but a DrawingBrush though. You can't set the Color property of a SolidColorBrush to a DrawingBrush.

If you want to use a Color in AppBrush, you'll need to define it like <SolidColorBrush x:Key="AppBrush" Color="{StaticResource SteelBrush}"/>
If you would like to add this brush to button, you can use the same way, like <Button Background="{StaticResource SteelBrush}"/> or create s Style for Button in your resource dictionary
<Style TargetType="{x:Type Button}" BasedOn="{StaticResource {x:Type Button}}" x:Key="StyleKey">
<Setter Property="Background" Value="{StaticResource SteelBrush}"/>
…
</Style>

Related

Wpf ResourceDictionary for Style and Brushes and how to MergedDictionaries

I hope someone can clarify the situation I have below.
I have a simple CustomControl style based on a Button in a ResourceDictionary named SHButtonStyle.xaml
<Style TargetType="{x:Type local:SHButton}">
<Setter Property="Background" Value="{StaticResource ResourceKey= Background}"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:SHButton}">
<Border Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
I also have a ResourceDictionary named Brushes as below:
<SolidColorBrush x:Key="Background" Color="Red"/>
I also have a Themes folder with Generic.xaml which has the MergedDictionaries as below:
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/TestCustomControl;component/SHButtonStyle.xaml"/>
</ResourceDictionary.MergedDictionaries>
I have tried merging the ResourceDictionary for the Brushes within the Generic.xaml as I understood it is best to merge all ResourceDictionary's within Generic.xaml?
But the only way I can get this to work is with an additional MergedDictionaries for the Brushes within the SHButtonStyle.xaml. Is this correct or what am I missing when merging the ResourceDictionary's within the Generic.xaml.
Thank you in advance for your assistance
Either merge the brushes resource dictionary in theme/generic.xaml directly, or merge them both at global level in App.xaml:
<Application x:Class="WpfApp1.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/TestCustomControl;component/Brushes.xaml"/>
<ResourceDictionary Source="/TestCustomControl;component/themes/generic.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
The MergedDictionaries of themes/generic.xaml usually contains "standalone" resource dictionaries for custom controls without any dependencies to resources in other resource dictionaries.

Exception: Cannot find resource named '*'. Resource names are case sensitive

But why. I see to be correct how the file works:
Error in IFocus.xaml but the converter is defined before.
I don't understant what is wrong.
Referance: Modern.xaml
Is a referance from another project. and i like this.
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:c="clr-namespace:Modern.Converters">
<SolidColorBrush x:Key="C_FocusBush" Color="Red"/>
<c:ThicknessConverter x:Key="ThicknessConverter"/>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Interfaces/IFocus/IFocus.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
The IFocus.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:i="clr-namespace:Modern.Interfaces">
<Style TargetType="{x:Type i:IFocus}" x:Key="{x:Type i:IFocus}">
<Setter Property="BorderBrush" Value="{DynamicResource C_FocusBush}"/>
<Setter Property="BorderThickness" Value="2"/>
<Setter Property="Padding" Value="2"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type i:IFocus}">
<Grid Margin="{TemplateBinding Padding}">
<Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness, Converter={StaticResource ThicknessConverter}"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
The main app to include all resources:
<Application x:Class="*.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/Modern;component/Modern.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
The brush works fine but the Converter not, why not?
I encountered an error format the same like this after I changed my Visual Studio theme to custom (mine's OneMonokai). I just reverted it back to the default theme and surprisingly it was resolved. It may seem far from the question but you could try this fix if you want.
Since it is the Style in IFocus.xaml that references a Brush resource in Modern.xaml, it's IFocus.xaml that should merge Modern.xaml and not the other way around:
Modern.xaml:
<ResourceDictionary ...>
<SolidColorBrush x:Key="C_FocusBush" Color="Red"/>
</ResourceDictionary>
IFocus.xaml:
<ResourceDictionary ...>
<Style ...>
<Setter Property="BorderBrush" Value="{StaticResource C_FocusBush}"/>
</Style>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="../../Modern.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
App.xaml:
<ResourceDictionary Source="pack://application:,,,/Modern;component/Interfaces/IFocus/IFocus.xaml"/>
Alternatively, you can create a separate resource dictionary with all brushes and merge this one and the one with the styles into App.xaml or another resource dictonary.
You may also want to see my answer here for more information about the order in which resource dictionaries are loaded.

WPF window's background color is not set by style automatically

I have created simple WPF application with one window. What I want is to apply background color automatically to all windows. However, the color isn't applied.
Here's link to sample project. The following is XAML in App:
<Application x:Class="SampleWPFApp.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:SampleWPFApp"
StartupUri="MainWindow.xaml">
<Application.Resources>
<Style TargetType="Window">
<Setter Property="Background">
<Setter.Value>
<SolidColorBrush Color="#FF3B444B" />
</Setter.Value>
</Setter>
</Style>
<Style TargetType="local:MainWindow" BasedOn="{StaticResource Window}" />
</Application.Resources>
</Application>
The logic was taken from here.
EDIT:
Well, I wasn't a bit fare about when the background isn't set - it's not set in VS editor. When program runs, background is OK. The solutions of Ragavan and mm8 do work, albeit they are the same with the difference that BasedOn="{StaticResource Window}" lets us omit the style's key (being Window the key itself).
Alas, the editor doesn't show the background, although setting the style explicitly (in MainWindow's XAML) makes background appear.
Basedon Will not bind directly window . Replace this code BasedOn="{StaticResource {x:Type Window}}"
App.Xaml
<Application x:Class="SampleWPFApp.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:SampleWPFApp"
StartupUri="MainWindow.xaml">
<Application.Resources>
<Style TargetType="Window">
<Setter Property="Background">
<Setter.Value>
<SolidColorBrush Color="#FF3B444B" />
</Setter.Value>
</Setter>
</Style>
<Style TargetType="local:MainWindow" BasedOn="{StaticResource {x:Type Window}}"/>
</Application.Resources>
</Application>
In WPF, you set the styling resource for a type and not for an instance of type. Hence, below line should be removed.
Style TargetType="local:MainWindow" BasedOn="{StaticResource Window}" />
In your case, Windows background is being applied but it is just not visible because it may have a panel within. For example, when you create a new window, it will have Grid by default. Add below line and you will get the background for entire window. Please append &LT character in the below lines.
&LTStyle TargetType="{x:Type Grid}" BasedOn="{x:Null}">
&LTSetter Property="Background" Value="Black" />
&LT/Style>
Just give your style an x:Key and base the MainWindow style on this one. This works wonders for me:
<Application x:Class="SampleWPFApp.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:SampleWPFApp"
StartupUri="MainWindow.xaml">
<Application.Resources>
<Style x:Key="myWindowStyle" TargetType="Window">
<Setter Property="Background">
<Setter.Value>
<SolidColorBrush Color="#FF3B444B" />
</Setter.Value>
</Setter>
</Style>
<Style TargetType="local:MainWindow" BasedOn="{StaticResource myWindowStyle}" />
</Application.Resources>
</Application>

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.

Trying to change window background color from XAML file

I'm playing around with the ExpressionDark.xaml theme. I'm setting the theme in App.xaml:
<Application x:Class="WpfApplication4.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary Source="Themes/ExpressionDark.xaml"/>
</Application.Resources>
</Application>
I looked at how they were setting the colors and styles of other controls, but am unable to produce the same result with the window.
If you like, you can see the XAML here.
Here's the XAML I'm trying:
<Style TargetType="{x:Type Window}">
<Setter Property="Background" Value="{DynamicResource BlackTestBrush}" />
<Style.Triggers>
</Style.Triggers>
</Style>
<SolidColorBrush x:Key="BlackTestBrush" Color="#FF000000" />
Any ideas on what I'm doing wrong here?
Thanks

Resources