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.
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 am using MahApps and working on implementing validation for TextBoxes. MahApps provides some nice properties in TextBoxes, as Controls:TextBoxHelper.Watermark and Controls:TextBoxHelper.ClearTextButton. I am writing an ErrorTemplate using my style but I overwrite the default template of the TextBox and lose those Metro properties. How can I achieve my goal without losing the template:
<Style TargetType="TextBox">
<Setter Property="Validation.ErrorTemplate" Value="{DynamicResource ErrorToolTipTemplate}" />
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="true">
<Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors)[0].ErrorContent}" />
</Trigger>
</Style.Triggers>
</Style>
You should define your new style based on existing to keep everything it has. I guess in case of MahApps it will be MetroTextBox:
<Style TargetType="TextBox" BasedOn="{StaticResource MetroTextBox}">
<!-- your properties go here -->
</Style>
The BasedOn is the key to success. However, it's likely, that you're using MahApps controls not in just one place. Therefore, try to make your style more generic and avoid direct references to MetroTextBox resource in the BasedOn.
The more good looking XAML style would be like that:
<Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
<!-- your properties go here -->
</Style>
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 have custom control that extends DataGrid. It is called ExtendedDataGrid. I want to provide style for ExtendedDataGrid that is the same as DataGrids style except it changes the template. I have tried something like this:
<Style TargetType="{x:Type MyControls:ExtendedDataGrid}" BasedOn="{StaticResource {x:Type DataGrid}}">
<Setter Property="Template">
...
</Setter>
</Style>
But it says that that the resource is not found.
So I try:
<Style TargetType="{x:Type MyControls:ExtendedDataGrid}" BasedOn="{StaticResource {ComponentResourceKey ResourceId=DataGridStyle, TypeInTargetAssembly={x:Type DataGrid}}}">
<Setter Property="Template">
...
</Setter>
</Style>
But it also does not work... So what do I do ?
Well mystery is solved :)
My first code above actually works:
<Style TargetType="{x:Type MyControls:ExtendedDataGrid}" BasedOn="{StaticResource {x:Type DataGrid}}">
<Setter Property="Template">
...
</Setter>
</Style>
I thought that it is not working becase VS (or Resharper) showed error in my code saying that resource is not found... Bug in VS (or Resharper) :(
If you create a style with a
TargetType property and base it on
another style that also defines a
TargetType property, the target type
of the derived style must be the same
as or be derived from the type of the
base style.
Your grid does inherit from DataGrid, right?