Reference Main Window's Dictionary from Resource Dictionary - c#

I have a WPF Window:
<Window x:Class="MyUI.MainWindow"
and so on>
<Window.Resources>
<ResourceDictionary>
<Style TargetType="{x:Type s:SurfaceListBox}" x:Key="FatherStyle" >
</ResourceDictionary>
</Window.Resources>
</Window>
I have a resource dictionary in MyResourceDictionary.xaml:
<ResourceDictionary xmlns="........."
and so on >
<Style TargetType="{x:Type s:SurfaceListBox}" x:Key="ChildStyle" BasedOn="{StaticResource FatherStyle}" />
</ResourceDictionary>
But when I try to reference ChildStyle from MyUI.Window:
<Window as shown in 1st block of code above>
<s:SurfaceListBox Style="{StaticResource ResourceKey=ChildStyle}" />
</Window>
It tells me that it can't find FatherStyle. I read here and merged the dictionary in MyResourceDictionary.xaml:
<ResourceDictionary xmlns="........."
and so on >
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="MainWindow.xaml" />
</ResourceDictionary.MergedDictionaries>
<Style ChildStyle as shown above />
</ResourceDictionary>
Now it tells me it can't find ChildStyle. How do I reference this properly?

You can't reference a resource dictionary contained in a Window-type XAML file, from another file. What you need to do is create a separate resource dictionary, "Shared.xaml" or whatever:
<ResourceDictionary ... >
<Style TargetType="{x:Type s:SurfaceListBox}" x:Key="FatherStyle" >
</ResourceDictionary>
Now reference the shared one from your main window:
... and also from "MyResourceDictionary.xaml":
<ResourceDictionary xmlns="........."
and so on >
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Shared.xaml" />
</ResourceDictionary.MergedDictionaries>
<Style TargetType="{x:Type s:SurfaceListBox}" x:Key="ChildStyle" BasedOn="{StaticResource FatherStyle}" />
</ResourceDictionary>
And now in your "MyUI.xaml" window, you should be able to access the "ChildStyle" as you expected, by referencing "MyResourceDictionary":
<ResourceDictionary xmlns="........."
and so on >
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="MyResourceDictionary.xaml" />
</ResourceDictionary.MergedDictionaries>
<s:SurfaceListBox Style="{StaticResource ResourceKey=ChildStyle}" />
</ResourceDictionary>

Related

How to apply MaterialDesignInXAML to only one control

I want to use the MaterialDesignInXAML for some elements of my WPF application, but i don't want to apply the theme to the whole apps (i'm particularly interested in the buttons)
So how do use the theme without applying this to App.xaml:
<Application . . . >
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Light.xaml" />
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Defaults.xaml" />
<ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Primary/MaterialDesignColor.DeepPurple.xaml" />
<ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Accent/MaterialDesignColor.Lime.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
You Should Add ResourceDictionary lines in your UserControl
<Button x:Class="MyButton">
<Button.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Light.xaml" />
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Defaults.xaml" />
<ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Primary/MaterialDesignColor.DeepPurple.xaml" />
<ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Accent/MaterialDesignColor.Lime.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Button.Resources>
</Button>
and use MyButton
<MyButton></MyButton>
Only reference the button resources: MaterialDesignTheme.Button.xaml:
<Application>
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Button.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
All the styles are not implicit and must be referenced explicitly as {StaticResource}. You can check the source file on GitHub to know which styles (keys) are available.

ResourceDictionary is not used as button style

I am trying to use ResourceDictionary to set up a style for a button
<Window.Resources>
<vieModel:MainWindowViewModel x:Key="MainViewModel"/>
<ResourceDictionary x:Key="ButtonStyle"> //If I don't use key it gives error
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="GlassButton.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
and using it as
<Button Style="{DynamicResource ButtonStyle}"/>
Now, when I do this it complains that ResourceDictionary can not be applied to Style. When I use "GlassButton" as defined in GlassButton.xaml it says that GlassButton could not be resolved.
when I use it like
<Button Style="{StaticResource ButtonStyle}"/>
or use GlassButton in both cases either it complains or wont work.
GlassButton.xaml looks like this, and compiling fine
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Citations">
<Style x:Key="GlassButton" TargetType="{x:Type Button}">
<Setter Property="FontSize" Value="42" />
<Setter Property="Foreground" Value="White" />
<Setter Property="Template">
I am taking this example from here or here both are kind of using same approach of using ResourceDictionary. I am doing as shown but it still not working. Do I need a converter for this? or I am doing something wrong?
The following worked for me. Check that the path to GlassButton.xaml is correct. If you put it in a folder such as Styles you would need to use Source="Styles/GlassButton.xaml".
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="GlassButton.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
<Grid>
<Button Style="{DynamicResource GlassButton}"
Content="Button Content"/>
</Grid>
I should be using it as follows
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="GlassButton.xaml"/>
</ResourceDictionary.MergedDictionaries>
<vieModel:MainWindowViewModel x:Key="MainViewModel"/>
</ResourceDictionary>
</Window.Resources>
This worked
Move the
<vieModel:MainWindowViewModel x:Key="MainViewModel"/>
line to inside of ResoruceDictonary like below:. Then you don't need to enter the key x:Key="ButtonStyle" and then style will be applied
<Window.Resources>
<ResourceDictionary >
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="GlassButton.xaml"/>
</ResourceDictionary.MergedDictionaries>
<vieModel:MainWindowViewModel x:Key="MainViewModel"/>
</ResourceDictionary>
</Window.Resources>

To call converter from ControlTemplate string

I'm trying to bind controlTemplate to in generic.xaml.My controlTemplate has converter in it.While binding,its throwing an exception as Provide value on 'System.Windows.Markup.StaticResourceHolder' threw an exception.
In MyView.cs
templateString = #"<ControlTemplate xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation"" xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"" >
<Canvas><Polygon Points=""{Binding Size,Converter={StaticResource SizeConverter}}"" Fill=""red""/></Canvas>
</ControlTemplate>";
this.Template = XamlReader.Load(new System.Xml.XmlTextReader(new StringReader(templateString ))) as ControlTemplate;
In generic.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:src="clr-namespace:MySample"
xmlns:vsm="clr-namespace:System.Windows;assembly=PresentationFramework"
xmlns:srcview="clr-namespace:MySample.Views"
xmlns:converters="clr-namespace:MySample.Converters"
>
<converters:SizeConverter x:Key="SizeConverter" />
<Style TargetType="{x:Type srcview:MyView}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type srcview:MyView}">
<Canvas>
<ContentControl Template="{TemplateBinding Template}" Name="contentControl" >
</ContentControl>
</Canvas>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
How to fix this issue??
One normally gets this problem when the static resources are not defined before referencing them.
For a test, try reading an xaml usercontrol with a static resource defined in the resources section of the xaml. If that works then you know the problem is not with referencing the converter, but rather when and were it is defined.
A faster way to ensure that the converter is actually being loaded is to put it in the app.xaml. This will ensure that the resource dictionary is loaded at startup. Here follows an example:
<Application x:Class="TeslaFrame.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:metro="http://schemas.codeplex.com/elysium"
>
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Elysium;component/Themes/Generic.xaml"/>
<ResourceDictionary>
<BooleanToVisibilityConverter x:Key="globalBoolToVisConverter" />
</ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>

MergedDictionaries nightmare

I'm trying to use a WPF Template and I'm supposed to include this in my App.xaml, but I get "Nested properties are not supported: ResourceDictionaries.MergedDictionaries" error and "The attachable property "MergedDictionaries" was not found in type "ResourceDictionary".
<Application x:Class="Unico.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.MergedDictionaries>
<!-- Set default skin -->
<ResourceDictionary Source="\ExpressionDark.xaml"/>
<ResourceDictionary Source="\WindowStyle.xaml"/>
</ResourceDictionary.MergedDictionaries>
<Style TargetType="{x:Type Rectangle}" />
</Application.Resources>
</Application>
I've tried everything but still can't fix this. Any idea? Thanks.
MergedDictionaries is a property of ResourceDictionary.
Change to:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
...
</ResourceDictionary.MergedDictionaries>
<Style TargetType="{x:Type Rectangle}" />
</ResourceDictionary>
</Application.Resources>

Silverlight. Cannot set base style from other xaml resources

I need to create resource with name OkButtonStyle based on RedButtonStyle.
But i have resource with key not found exception. What i am doing wrong?
I have two resouce dictionaries. One baseStyles.xaml where RedButtonStyle located and styles.xaml where i need to locate my okbuttonstyle but all my efforts results nothing.
App.xaml
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="baseStyles.xaml" />
<ResourceDictionary x:Name="currentTheme" Source="styles.xaml" />
</ResourceDictionary.MergedDictionaries>
styles.xaml
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="baseStyles.xaml" />
</ResourceDictionary.MergedDictionaries>
<Style x:Key="OkButtonStyle"
BasedOn="RedButtonStyle"
TargetType="Button">
</Style>
baseStyles.xaml
<Style x:Key="RedButtonStyle"
TargetType="Button">
...properties...
</Style>
Instead of using the syntax:
<Style BasedOn="RedButtonStyle" ...
use the syntax:
<Style BasedOn="{StaticResource RedButtonStyle}" ...
The Style.BasedOn property is not the name of the other style it is based on, it is the style it is based on.

Categories

Resources