I added a font file to the project, and I want to put it in the resource dictionary.
But it can't be identified correctly. I think it can only recognize the font name.
code
<FontFamily x:Key="anticon-uri">pack://application:,,,/Ant.WPF;component/Resources/#anticon</FontFamily>
I've tried the following method, it's OK.
```
<Style x:Key="anticon" TargetType="TextBlock">
<Setter Property="FontFamily" Value="pack://application:,,,/Ant.WPF;component/Resources/#anticon" />
</Style>
<Style BasedOn="{StaticResource anticon}" TargetType="TextBlock">
<Setter Property="FontSize" Value="20" />
</Style>
```
Do you have a better way?
I just found the answer.
syntax: <FontFamily x:Key="anticon-uri">ResourceFile FontName</FontFamily>
Example:
<FontFamily x:Key="anticon-uri">pack://application:,,,/Ant.WPF;component/Resources/#anticon anticon</FontFamily>
Related
so I'm trying to build an application where I'm using some of the ButtonAdv-controls.
As I'm also coding some triggers connected to it, I've to access the style.
However, I can't get the basedon attribute working on this one.
<Style x:Key="SyncButtonAdvMainPage" TargetType="{x:Type syncfusion:ButtonAdv}" BasedOn="{StaticResource MetroButtonAdvStyle}">
<Setter Property="Margin" Value="2"/>
<Setter Property="HorizontalContentAlignment" Value="Left"/>
<Setter Property="BorderThickness" Value="0"/>
<Setter Property="SizeMode" Value="Normal"/>
<Setter Property="IconHeight" Value="30"/>
</Style>
Gives me an error when using it for a control that the ressource couldn't be resolved.
It works for regular (not Syncfusion) buttons tough as I'm referencing the style and the theme in App.xaml.
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Syncfusion.Shared.WPF;component/SkinManager/MetroStyle.xaml"/>
</ResourceDictionary.MergedDictionaries>
<Style x:Key="MetroButtonBase" TargetType="{x:Type Button}" BasedOn="{StaticResource MetroButtonStyle}">
<Setter Property="Margin" Value="5" />
</Style>
</ResourceDictionary>
Some ideas on this?
Thanks and greetings.
I have checked your code. In this you have merged MS control theme resource to Syncfusion ButtonAdv control. To override Syncfusion ButtonAdv control, you can use the below path.
<ResourceDictionary Source="/Syncfusion.Shared.WPF;component/Controls/ButtonControls/Button/Themes/MetroStyle.xaml" />
You can get the resource path for Syncfusion controls in below link.
https://help.syncfusion.com/wpf/skinmanager/overview#resourcedictionary-path-for-syncfusion-themes
I have override ButtonAdv control style with BasedOn property to use Metro ButtonAdv Style and created simple sample. Please find it below.
Sample: http://www.syncfusion.com/downloads/support/directtrac/general/ze/ButtonAdvSample-422062150180166807
Regards,
Keerthana J.
Relating on this question (Multiple styles on same target type) i have another point concerning the general wpf styling rules.
As mentioned in one of the answers above, wpf applies the closest style definition to the control when no x:key is specified. (also applies to styles with the same key; given the fact that we have the same key here: the control type)
My question:
What if we have two same-named styles in different resource dictionaries that are both equal close and accessible to the control?
Compared to CSS you have two definitions for i.e. an a-element. In CSS same style properties are getting overriden by the last definition while different properties are merged into one logical style definition.
Does WPF behave the same or does it prefer one whole style-resource with all its properties while avoiding the other one completely?
And if the latter how does it decide to apply one or the other.
Update
dict1.xaml:
<ResourceDictionary>
<Style TargetType="{x:Type Button}" BasedOn="{StaticResource {x:Type Button}}">
<Setter Property="BorderBrush" Value="SomeValue1" />
<Setter Property="Background" Value="SomeValue2" />
<Setter Property="FontSize" Value="SomeValue3" />
</Style>
</ResourceDictionary>
dict2.xaml:
<ResourceDictionary>
<Style TargetType="{x:Type Button}" BasedOn="{StaticResource {x:Type Button}}">
<Setter Property="BorderBrush" Value="SomeValue4" />
<Setter Property="Foreground" Value="SomeValue5" />
</Style>
</ResourceDictionary>
SomeView.xaml:
...
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="path/dict1.xaml" />
<ResourceDictionary Source="path/dict2.xaml" />
</ResourceDictionary.MergedDictionaries>
<Grid>
<Grid.Resources>
<Style TargetType="{x:Type Button}" BasedOn="{StaticResource {x:Type Button}}">
<Setter Property="FontSize" Value="SomeValue6" />
</Style>
</Grid.Resources>
<Button /><!--THIS IS THE CONTROL THAT MATTERS-->
</Grid>
...
I think for FontSize its simple, it applies SomeValue6 because its the closest style definition. But what about BorderBrush. Both Dictionaries are equally close to the Button and are both accessible.
And What about Background and Foreground? Does it merge both style definitions to something like this:
<ResourceDictionary>
<Style TargetType="{x:Type Button}" BasedOn="{StaticResource {x:Type Button}}">
<Setter Property="BorderBrush" Value="SomeValue4" />
<Setter Property="Background" Value="SomeValue2" />
<Setter Property="Foreground" Value="SomeValue5" />
<Setter Property="FontSize" Value="SomeValue6" />
</Style>
</ResourceDictionary>
Of course the chaos is much bigger when you include cross-assembly dictionaries, that depend on each other and you are seeking where the actual style comes from.
This code example isn't working because of the merged dictionary and its hard to isolate the exact behavior because my actual application is really chaotic like I described.
So I want to know the official rules intended by Microsoft that should be basic for every developer. But I didnt find anything in the docs.
I have two styles
<Style x:Key="FontElemNivel1">
<Setter Property="TextElement.FontSize" Value="12"/>
<Setter Property="TextElement.FontFamily" Value="Tahoma"/>
<Setter Property="TextElement.FontWeight" Value="Bold"/>
</Style>
And this
<Style x:Key="ElementoNivel1" TargetType="TextBlock">
<Setter Property="Style" Value="{StaticResource FontElemNivel1}"/>
<Setter Property="HorizontalAlignment" Value="Left"/>
</Style>
If I try to use the second on an object like this
<TextBlock Text="Entidad" Style="{DynamicResource ElementoNivel1}"/>
The compiler throw this error:
Error 16 the Style object cannot affect the Style property of the object to which it applies.
Why this happens ?
How to implement it properly ?
<Style x:Key="ElementoNivel1" TargetType="TextBlock" BasedOn="{StaticResource FontElemNivel1}">
This should rectify an error. You tried to assign style to style.
For BasedOn to work FontElemNivel1 will need to target a Textblock.
If doesn't suit because FontElemNivel1 needs to be used for something other than textblocks then maybe this previously answered question will help:
XAML Combine Styles
I'd like to implement a sort of Factory Pattern for XAML. I created an app for WinRT where I defined two xaml style files. Basically, what I'd like to achieve (if possible) is to load one of the the two xaml file when the application starts.
in the solution explorer I have this:
CustomStyles foder contains the style files. So, based on an enumerator in my App.xaml.cs file
public enum Style
{
Style_1,
Style_2
}
if I choose Style_1 I'd like to load the xaml file Style_1.xaml else Style_2.xaml at run-time.
Both the style files, have the same definition of Button style, TextBlock style, etc with different property values.
Here an example:
Style_1.xaml
<Style x:Key="Attribute_Label" TargetType="TextBlock">
<Setter Property="FontFamily" Value="Segoe UI" />
<Setter Property="Foreground" Value="#78CAB3" />
<Setter Property="FontSize" Value="15" />
<Setter Property="FontWeight" Value="Normal" />
</Style>
Style_2.xaml
<Style x:Key="Attribute_Label" TargetType="TextBlock">
<Setter Property="FontFamily" Value="Arial" />
<Setter Property="Foreground" Value="#606060" />
<Setter Property="FontSize" Value="30" />
<Setter Property="FontWeight" Value="Normal" />
</Style>
There is a way to achieve what I want to do ? Thank you in advance.
We end up to do something like this:
defining a ResourcesDictionary in App.xaml with all the CustomStyles
we make a server request that decides which custom style has to be loaded
using this piece of code Application.Current.Resources[CustomStyleVariable]; we load the whole style in a Style object.
We haven't found any better solution yet but, it seems to work.
When I use the following code it works because I am using a ListBox
<UserControl.Resources>
<Style BasedOn="{StaticResource {x:Type ListBox}}" TargetType="{x:Type ListBox}">
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
<Setter Property="Background" Value="Transparent" />
</Style>
</UserControl.Resources>
But when I use the following code to a ListView I get an warning/exception
<UserControl.Resources>
<Style BasedOn="{StaticResource {x:Type ListView}}" TargetType="{x:Type ListView}">
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
<Setter Property="Background" Value="Transparent" />
</Style>
</UserControl.Resources>
"StaticResource reference 'System.Windows.Controls.ListView' was not found."
Why and how to solve it? I want the functionality of a ListView.
Try this one:
<Style BasedOn="{StaticResource {x:Type ListBox}}" TargetType="{x:Type ListView}">
A ListView does not handle presentation, it delegates this to its View property, which is usually a GridView. Try setting the style using the GridView type as key.
I'm having the same problem. my scenario is little complicated: app1 references lib1 and lib2, lib1 defines the listview style, lib2 defines a user control containing a listview and has no idea about the listview style. app1 adds the resource dictionary:
in lib2, I define list view like this:
at design time, I got the error:
StaticResource reference 'System.Windows.Controls.ListView' was not found.
but at runtime, it works well.
I guess this is a bug of VS2008.
I tried it on VS2010 which does not have this design time error.