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

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.

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.

how to create a resource based on resource dictionary in WPF

I create a resource dictionary that receives 2 parmeters : releasedImage and PressedImage :
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:SwitchesLibrary">
<Style TargetType="local:ImageButton" BasedOn="{StaticResource {x:Type Button}}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="local:ImageButton">
<Grid>
<Image x:Name="PressedButton"
Source="{TemplateBinding PressedImage}" />
<Image x:Name="ReleasedButton"
Source="{TemplateBinding ReleasedImage}" />
</Grid>
</Setter.Value>
</Setter>
</Style>
In another lib, I will use several buttons with the same images. So I want to create a resource in this lib with specific PressedImage and ReleasedImage,
like this :
<UserControl x:Class="ExamplePanelLibrary.ExamplePanelControl"
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:ExamplePanelLibrary"
xmlns:SwitchesLibrary="clr-namespace:SwitchesLibrary;assembly=SwitchesLibrary"
mc:Ignorable="d"
d:DesignHeight="760" d:DesignWidth="754">
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary
Source="pack://application:,,,/SwitchesLibrary;component/ImageButtonStyle.xaml">
</ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
<ImageBrush x:Key="ButtonPressed" ImageSource="Images/PushButtons/OSB_Pushed.png"/>
<ImageBrush x:Key="ButtonReleased" ImageSource="Images/PushButtons/OSB_Released.png"/>
<Style
x:Key="OSBPushButton"
TargetType="SwitchesLibrary:ImageButton"
ReleasedImage="Images/SpecificButtonReleased.png"
PressedImage="Images/SpecificButtonPressed.png"
/>
</ResourceDictionary>
Can we do something like that ?
This may work:
<Style x:Key="OSBPushButton"
TargetType="SwitchesLibrary:SpecificImageButton"
BasedOn="{StaticResource {x:Type local:ImageButton}}">
<Setter Property="ReleasedImage" Value="Images/SpecificButtonReleased.png"/>
<Setter Property="PressedImage" Value="Images/SpecificButtonPressed.png"/>
</Style>
Clemens gave the correct answer. I just want to rewrite this clearly :
<Style
x:Key="OSBPushButton"
TargetType="SwitchesLibrary:ImageButton"
BasedOn="{StaticResource {x:Type SwitchesLibrary:ImageButton}}">
<Setter Property="ReleasedImage" Value="Images/SpecificButtonReleased.png"/>
<Setter Property="PressedImage" Value="Images/SpecificButtonPressed.png"/>
</Style>
and use it like this :
<SwitchesLibrary:ImageButton x:Name="OSB_1" Style="{StaticResource OSBPushButton}"/>

MahApps Metro setting Window Border styles from App Resources

I am trying to set Window Border styles for my MahApps Metro app. I have read the articles about how to set the different Border styles and I think I get it. However, I am trying to set the Border Style for all windows in my app to be the same (all Drop Shadow) and it doesn't seem to want to work.
I have app resources that look like this:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/GSDXThemes;component/GSDXDarkYellowTheme.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
My Resource dictionary looks like this:
<!-- Merge in ResourceDictionaries defining base styles to use. This theme is based on the Metro Dark Yellow theme. -->
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Colors.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/Yellow.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/BaseDark.xaml" />
<ResourceDictionary Source="pack://application:,,,/GSDXThemes;component/GSDXControlStyles.xaml" />
</ResourceDictionary.MergedDictionaries>
The GSDXControlStyles dictionary just sets some custom style values for my app. It is in this file that I try to set the Window Borders.
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:resx="clr-namespace:GSDXThemes.Properties"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:System="clr-namespace:System;assembly=mscorlib"
xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
xmlns:GSDUserControls="clr-namespace:GSD.CommonGUI.UserControls;assembly=CommonGUI">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" />
</ResourceDictionary.MergedDictionaries>
<!-- Now customize the theme for our use...mostly just changing font sizes, etc...-->
<Style TargetType="{x:Type Controls:MetroWindow}" >
<Setter Property="WindowTransitionsEnabled" Value="False" />
<Setter Property="EnableDWMDropShadow" Value="True" />
</Style>
<Style TargetType="{x:Type Label}" BasedOn="{StaticResource MetroLabel}">
<Setter Property="FontSize" Value="16"/>
</Style>
<Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource MetroTextBox}">
<Setter Property="FontSize" Value="16"/>
</Style>
...
All the other style settings work fine. But the first line for setting the Window Border does nothing. All my windows show with no border.
How can I get this to work so all Windows have the Drop Shadow border?
you must give your style a key to get a working solution
<Style x:Key="CustomDefaultWindowStyle"
TargetType="{x:Type Controls:MetroWindow}"
BasedOn="{StaticResource {x:Type Controls:MetroWindow}}" >
<Setter Property="WindowTransitionsEnabled" Value="False" />
<Setter Property="EnableDWMDropShadow" Value="True" />
</Style>
now use this style on all your MetroWindows
<Controls:MetroWindow x:Class="YourWindowClass"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
xmlns:System="clr-namespace:System;assembly=mscorlib"
Title="Custom Window Style Demo"
Height="600"
Width="800"
WindowStartupLocation="CenterScreen"
Style="{DynamicResource CustomDefaultWindowStyle}">
...
</Controls:MetroWindow>
(don't be afraid of the'Invalid style target type:...' message, it's a VS bug)
hope that helps

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

wpf merge multiple styles into one style

I have a UserControl that consists of a listview it looks like:
<UserControl
....
<UserControl.Resources>
<Style TargetType="Thumb">
<!-- Style Content -->
</Style>
<Style TargetType="GridViewColumnHeader">
<!-- Style Content -->
</Style>
<Style TargetType="{x:Type ScrollBar}">
<!-- Style Content -->
</Style>
<Style TargetType="{x:Type ScrollViewer}">
<!-- Style Content -->
</Style>
<Style TargetType="{x:Type ListViewItem}">
<!-- Style Content -->
</Style>
</UserControl.Resources>
<ListView Name="ListView1" >
<!-- ListViewContent -->
</Style>
</UserControl>
I have 3 of those userControls where the only thing that is different between them is the styles in <UserControl.Resources>. It makes no scene to have to create multiple controls that have the same functionality just because I need a different look and feel. What I want to do now is combine all the styles in <UserControl.Resources> into one style. If I manage to group all those styles into one I would be able to remove the 3 controls and change the style as:
<ListView Style={DynamicResource style1} ...
Currently if I do
<UserControl.Resources>
<Style x:Key="style1">
<!-- Place all styles in here -->
</Style>
</UserControl.Resources>
It does not work.
Edit
Thanks to iltzortz answer I now have:
Dictionary1.xaml:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style TargetType="Grid">
<Setter Property="Background" Value="Green"></Setter>
</Style>
<SolidColorBrush x:Key="Foo" Color="Red"></SolidColorBrush>
</ResourceDictionary>
Dictionary2.xaml:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style TargetType="Grid">
<Setter Property="Background" Value="Black"></Setter>
</Style>
<SolidColorBrush x:Key="Foo" Color="Orange"></SolidColorBrush>
</ResourceDictionary>
MyUserControl:
<UserControl x:Class="WpfApplication1.UserControl1"
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"
mc:Ignorable="d"
d:DesignHeight="97" d:DesignWidth="91">
<UserControl.Resources>
<ResourceDictionary Source="Dictionary1.xaml" ></ResourceDictionary>
</UserControl.Resources>
<Grid >
<Ellipse Fill="{DynamicResource Foo}" />
</Grid>
</UserControl>
And I change resource dictionaries dynamically like this: switching wpf resource dictionaries at runtime
Add a resource dictionary to your application named e.g. common.xaml
and put your common styles there
then you can reuse it with:
<UserControl.Resources>
<ResourceDictionary Source="common.xaml"/>
</UserControl.Resources>
You can create 3 resource dictionaries and merge them at runtime. In my example code I used two resource dictionaries.
Example:
Dictionary1.xaml:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style x:Key="btnStyle" TargetType="Button">
<Setter Property="Margin" Value="0,10,0,0" />
</Style>
</ResourceDictionary>
Dictionary2.xaml:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style x:Key="btnStyle" TargetType="Button">
<Setter Property="Margin" Value="50,50,0,0" />
</Style>
</ResourceDictionary>
In the start of application you can set default style in App.xaml file:
<Application x:Class="Example.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="/Dictionary1.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
If you want to change style, you can merge resource dictionaries:
ResourceDictionary dict = new ResourceDictionary();
dict.Source = new Uri("\\Dictionary2.xaml", UriKind.Relative);
this.Resources.MergedDictionaries.Add(dict);
And now binding looks like this:
<Button Style="{DynamicResource btnStyle}" Content="Click me!" />
Now if you invoke code to merge resource dictionaries, button style will be automatically changed.

Resources