I have a resource dictionary with key BtnHyperLinkStyle inside the Window.Resources. I can't remove the key because it says every dictionary should have a key. Problem is when I say BasedOn, on a style tag it gives me an error saying that it can't find style named "HyperLinkButtonStyle" even though that style is present in StylesButton.xaml Please help. The ResourceDictionary source is correct.
<Window.Resources>
<ResourceDictionary x:Key="BtnHyperLinkStyle">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="ms-appx:///Presentation.Common/Styles/StylesButton.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
<Style x:Key="btnFlashButtonEnabledStyle" TargetType="Button" BasedOn="{StaticResource HyperLinkButtonStyle}"> <!--error here. it cant find the key-->
<Setter Property="Content">
<Setter.Value>
<Viewbox Height="40">
<Canvas Width="10240" Height="10240">
<Path Data="M6720 7533l0 1427c0,353 -287,640 -640,640l0 0 0 0 0 0c0,176 -216,320 -480,320l-960 0c-264,0 -480,-144 -480,-320l0 0 0 0 0 0c-353,0 -640,-287 -640,-640l0 -1427c139,49 286,78 427,98 382,53 787,49 1173,49 386,0 791,4 1173,-49 141,-20 288,-49 427,-98z" Fill="#252525"/>
<Path Data="M5120 320c1624,0 3200,1165 3200,2880 0,1600 -1280,2240 -1280,3200l0 320c0,640 -960,640 -1920,640 -960,0 -1920,0 -1920,-640l0 -320c0,-960 -1280,-1600 -1280,-3200 0,-1711 1572,-2880 3200,-2880zm642 715c-211,-49 -427,-75 -642,-75 -1266,0 -2560,884 -2560,2240 0,303 64,569 164,813 35,86 97,146 184,179 87,32 173,28 256,-14 149,-75 216,-252 153,-407 -73,-177 -117,-363 -117,-571 0,-994 1016,-1600 1920,-1600 164,0 331,20 496,58 146,35 292,-34 359,-168l0 -1c46,-90 47,-187 4,-279 -43,-91 -119,-152 -217,-175z" Fill="#A1C057"/>
</Canvas>
</Viewbox>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
The Style must be part of the of the same ResourceDictionary in order to reference resources from the ResourceDictionary.MergedDictionaries. You basically have the wrong object tree. When you use a ResourceDictionary as content of the Resources property it should be the only content:
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="ms-appx:///Presentation.Common/Styles/StylesButton.xaml"/>
</ResourceDictionary.MergedDictionaries>
<Style x:Key="btnFlashButtonEnabledStyle"
TargetType="Button"
BasedOn="{StaticResource HyperLinkButtonStyle}">
</Style>
</ResourceDictionary>
</Window.Resources>
You might want to try this. Move the Style into the ResourceDictionary and get rid of the x:Key.
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="ms-appx:///Presentation.Common/Styles/StylesButton.xaml"/>
</ResourceDictionary.MergedDictionaries>
<Style x:Key="btnFlashButtonEnabledStyle" TargetType="Button" BasedOn="{StaticResource HyperLinkButtonStyle}">
<Setter Property="Content">
<Setter.Value>
<Viewbox Height="40">
<Canvas Width="10240" Height="10240">
<Path Data="M6720 7533l0 1427c0,353 -287,640 -640,640l0 0 0 0 0 0c0,176 -216,320 -480,320l-960 0c-264,0 -480,-144 -480,-320l0 0 0 0 0 0c-353,0 -640,-287 -640,-640l0 -1427c139,49 286,78 427,98 382,53 787,49 1173,49 386,0 791,4 1173,-49 141,-20 288,-49 427,-98z" Fill="#252525"/>
<Path Data="M5120 320c1624,0 3200,1165 3200,2880 0,1600 -1280,2240 -1280,3200l0 320c0,640 -960,640 -1920,640 -960,0 -1920,0 -1920,-640l0 -320c0,-960 -1280,-1600 -1280,-3200 0,-1711 1572,-2880 3200,-2880zm642 715c-211,-49 -427,-75 -642,-75 -1266,0 -2560,884 -2560,2240 0,303 64,569 164,813 35,86 97,146 184,179 87,32 173,28 256,-14 149,-75 216,-252 153,-407 -73,-177 -117,-363 -117,-571 0,-994 1016,-1600 1920,-1600 164,0 331,20 496,58 146,35 292,-34 359,-168l0 -1c46,-90 47,-187 4,-279 -43,-91 -119,-152 -217,-175z" Fill="#A1C057"/>
</Canvas>
</Viewbox>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
</Window.Resources>
Related
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.
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}"/>
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>
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.
I want every button to have 5 points margin, in addition to Royale theme style.
Window1.xaml:
<Window x:Class="_styles.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/PresentationFramework.Royale;component/themes/royale.normalcolor.xaml" />
</ResourceDictionary.MergedDictionaries>
<Style TargetType="{x:Type Button}" BasedOn="{StaticResource {x:Type Button}}">
<Setter Property="Margin" Value="5"/>
</Style>
</ResourceDictionary>
</Window.Resources>
<StackPanel>
<Button Content="Button A"/>
<Button Content="Button B"/>
</StackPanel>
</Window>
It compiles but I get:
An unhandled exception of type 'System.StackOverflowException' occurred in PresentationFramework.dll
public Window1() {
InitializeComponent(); // <-- getting exception here
}
There are no exception details because:
{Cannot evaluate expression because the current thread is in a stack overflow state.}
This seems to be a circular reference between your style and the one defined in PresentationFramework.Royale. A workaroud would be to place resources at different levels:
<Window x:Class="_styles.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Window1" Height="300" Width="300">
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/PresentationFramework.Royale;component/themes/royale.normalcolor.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
<StackPanel>
<StackPanel.Resources>
<Style TargetType="{x:Type Button}" BasedOn="{StaticResource {x:Type Button}}">
<Setter Property="Margin" Value="5"/>
</Style>
</StackPanel.Resources>
<Button Content="Button A"/>
</StackPanel>
</Window>
I would remove the BasedOn attribute - it's not necessary. Think of it this way, merging the Royale theme will apply the button theme, and you just want to change the margin - styles are additive in nature, so it will combine the Royale theme and your own button theme without specifying the BasedOn attribute - does that make sense?
Cheers!
Please see this question and my answer for another solution that doesn't require you to specify a resource dictionary in every window and allows you to resolve the BasedOn style dynamically.