how to create a resource based on resource dictionary in WPF - 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}"/>

Related

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.

Extend style without setting an x:Key attribute in the base style

I have a file MyButtonStyles.xaml which designs the WPF button. This file uses a style to set some colors and fonts:
<ResourceDictionary xmlns......>
<Style BasedOn="{StaticResource {x:Type Button}} TargetType="{x:Type Button}">
<Setter Property="Foreground" Value="Blue" />
<Setter Property="FontSize" Value="22" />
</Style>
</ResourceDictionary>
This button is used in two xaml files. One shows the button as designed in the above style. This happens automatically because the above style has the according TargetType and it does not have an x:Key attribute.
In the other xaml file I use this button as well but the style from above should be extended by another setter property. Doing this by merging the dictionaries and basing on the original style it works:
<ResourceDictionary>
<ResourceDictionary.MergedDictionary>
<ResourceDictionary Source="MyButtonStyles.xaml" />
<ResourceDictionary.MergedDictionary>
<Style BasedOn="ButtonStylesOrig" TargetType="{x:Type Button}">
<Setter Property="Background" Value="Green" />
</Style>
</ResourceDictionary>
But for this I have to add an x:Key attribute (ButtonStylesOrig) to the base style. This means that in the first xaml which uses the button the base style will not be applied any more.
Is there a possibility to extend a style without losing the global scope of it (e.g. without using x:Key)?
This works:
<Window x:Class="WpfApplication1.Window1"
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"
mc:Ignorable="d"
Title="Window1" Height="300" Width="300">
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="MyButtonStyles.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
<Grid>
<Grid.Resources>
<Style TargetType="{x:Type Button}" BasedOn="{StaticResource {x:Type Button}}">
<Setter Property="Background" Value="Green" />
</Style>
</Grid.Resources>
<Button Content="Button" />
</Grid>
</Window>
The key is not to override the resource in the same resource dictionary that you merge your base style into:
WPF Using multiple Resource Dictionaries from multiple projects
You can't combine multiple default styles of the same type a single resource scope. However, it is possible to build default styles in nested resource scopes.
Suppose you merge MyButtonStyles.xaml into the App.xaml resources. Then you can place your second style with the additional setter into Window.Resources or other deeper nested resources and it will combine the correct implicit styles.
A more localized example:
<Grid>
<Grid.Resources>
<ResourceDictionary Source="MyButtonStyles.xaml"/>
</Grid.Resources>
<Grid>
<Grid.Resources>
<Style BasedOn="{StaticResource {x:Type Button}}" TargetType="{x:Type Button}">
<Setter Property="Background" Value="Green" />
</Style>
</Grid.Resources>
<Button VerticalAlignment="Top" Margin="20">Both styles</Button>
</Grid>
<Button VerticalAlignment="Center" Margin="20">ExampleDictionary style</Button>
</Grid>

Modern UI Pie Chart Doesn't Show Data

I am following all steps listed on this link,
https://modernuicharts.codeplex.com/documentation
but it gives below error and only title and subtitle show up no chart
Severity Code Description Project File Line Suppression State
Error 'ChartBase' TargetType does not match type of element
'PieChart'. MteroChartsModern c:\users\sesa388372\documents\visual
studio
2015\Projects\MteroChartsModern\MteroChartsModern\Application.xaml 9
My xaml code:
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<chart:PieChart
Style="{StaticResource MinimalChartStyle}"
ChartTitle="Minimal Pie Chart"
ChartSubTitle="Chart with fixed width and height"
SelectedItem="{Binding Path=SelectedItem, Mode=TwoWay}" >
<chart:PieChart.Series>
<chart:ChartSeries
SeriesTitle="Errors"
DisplayMember="Category"
ValueMember="Number"
ItemsSource="{Binding Path=Errors}" />
</chart:PieChart.Series>
</chart:PieChart>
</Grid> </Window>
Application.xaml
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary >
<Style x:Key="MinimalChartStyle" TargetType="chart:ChartBase">
<Setter Property="Width" Value="500"/>
<Setter Property="Height" Value="500"/>
</Style>
</ResourceDictionary>
</Application.Resources>
The issue is caused since you are defining the Style for TargetType="chart:ChartBase" and what you are using is chart:PieChart.
Please define a style for chart:PieChart like :
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary >
<Style x:Key="MinimalChartStyle" TargetType="chart:PieChart">
<Setter Property="Width" Value="500"/>
<Setter Property="Height" Value="500"/>
</Style>
</ResourceDictionary>
</Application.Resources>

Make all windows WindowStartupLocation default to CenterScreen

I had asked this question the other day. Now, I'm trying to apply the same practice to make all my windows, and have CenterScreen as it's default WindowStartupLocation. I've tried typing:
<Style TargetType="Window">
<Setter Property="WindowStartupLocation">
</Setter>
</Style>
However, apparently WindowStartupLocation isn't a supported property for this. Is there a way to accomplish this that I'm missing, or am I going to have to manually change this for all windows?
App.xaml
<Application.Resources>
<ResourceDictionary>
<Style x:Key="WindowStyle" TargetType="Window">
<Setter Property="SizeToContent" Value="WidthAndHeight" />
<Setter Property="ResizeMode" Value="CanMinimize" />
</Style>
<WindowStartupLocation x:Key="WSL">CenterOwner</WindowStartupLocation>
</ResourceDictionary>
</Application.Resources>
Window
<Window x:Class="WpfApplication7.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Width="525" Height="350" WindowStartupLocation="{StaticResource WSL}" Style="{StaticResource WindowStyle}">
<Window.Resources />
<Grid />
</Window>

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