I just realized that deriving from default style fails in my WPF app and I have no idea, why. Actually it works, but only due to "Hot reload". So I have:
<Style TargetType="ComboBox" x:Key="TestStyle" BasedOn="{StaticResource {x:Type ComboBox}}"/>
<Style TargetType="ComboBox">
<Setter Property="Width" Value="100"/>
</Style>
in resource dictionary, and:
<ComboBox Style="{StaticResource TestStyle}">
<ComboBoxItem>test</ComboBoxItem>
<ComboBoxItem>I want to cry with blood</ComboBoxItem>
</ComboBox>
in my control. When I start app I see following:
And when I remove BasedOn="{StaticResource {x:Type ComboBox}}" and add it again, I have correct view:
What can be a reason of such behavior? Seems like WPF bug, but I don't think this is possible
To find correct style you need to define it before "TestStyle"
<Style TargetType="ComboBox">
<Setter Property="Width" Value="100"/>
</Style>
<Style TargetType="ComboBox" x:Key="TestStyle" BasedOn="{StaticResource {x:Type ComboBox}}"/>
Related
I installed Material Design In XAML Toolkit to my project. I have ListView which contains within itself GridView (with GridViewColumns) and i want to override styles for each row in this table. But in each case i lose styles from Material Design In XAML Toolkit.
I tried do several things:
1) Override existing styles based on target type:
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem" BasedOn="{StaticResource {x:Type ListViewItem}}">
<Setter Property="Background" Value="Green" />
</Style>
</ListView.ItemContainerStyle>
I got overriding styles, but in this case i lose type recognition in GridView (Columns contains correct headers, but values contains call result ToString() method my model)
2) I used concrete style from Material Design In XAML Toolkit - MaterialDesignGridViewItem:
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem" BasedOn="{StaticResource MaterialDesignGridViewItem">
<Setter Property="Background" Value="Green" />
</Style>
</ListView.ItemContainerStyle>
In this case i got work solution (it would seem), but when i do adding triggers instead , i lose material styles (got only color, without animations).
3) In other cases i lose all material styles and go back to wpf default styles.
Hope on our help.
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem" BasedOn="{StaticResource MaterialDesignListBoxItem">
<Setter Property="Background" Value="Green" />
</Style>
</ListView.ItemContainerStyle>
Instead of using MaterialDesignGridViewItem, your extended style should be based on MaterialDesignListBoxItem.
The same works for other items. This helped me with TreeViewItem's that used to be in the style of MaterialDesign, but were also overwritten until I added the BasedOn property.
<TreeView.ItemContainerStyle>
<Style TargetType="{x:Type TreeViewItem}" BasedOn="{StaticResource MaterialDesignTreeViewItem}">
<EventSetter Event="TreeViewItem.DragOver" Handler="treeView_DragOver"/>
<EventSetter Event="TreeViewItem.Drop" Handler="treeView_Drop"/>
<EventSetter Event="TreeViewItem.MouseMove" Handler="treeView_MouseMove"/>
<EventSetter Event="TreeViewItem.MouseLeftButtonDown" Handler="treeView_MouseDown"/>
<EventSetter Event="TreeViewItem.MouseRightButtonDown" Handler="treeView_MouseRightDown"/>
</Style>
</TreeView.ItemContainerStyle>
Please note a "}" is missing after "MaterialDesignListBoxItem", so that:
<Style TargetType="ListViewItem" BasedOn="{StaticResource MaterialDesignListBoxItem}">
Why do my Radiobutton not look like a togglebutton?
Take a look at the code
<Style x:Key="ButtonBaseStyle" TargetType="{x:Type ButtonBase}">
<Setter Property="Height" Value="100" />
</Style>
<Style BasedOn="{StaticResource ButtonBaseStyle}" TargetType="{x:Type Button}" />
<Style BasedOn="{StaticResource ButtonBaseStyle}" TargetType="{x:Type ToggleButton}" />
<Style BasedOn="{StaticResource {x:Type ToggleButton}}" TargetType="{x:Type RadioButton}" />
<StackPanel>
<Button>Button</Button>
<ToggleButton>Toggle</ToggleButton>
<RadioButton>Radio</RadioButton>
</StackPanel>
If I remove the buttonbase style it works
The looks of your RadioButton is not as ToggleButton cause of you have now changed the style hierarchy of ButtonBase->ToggleButton->RadioButton (the order in which styles and templates are overwritten in derived classes of WPF).
Your new hierarchy of style gives the most priority to the style of ButtonBase. So you have a new ToggleButton style that is derived from a ButtonBase and then you overwrite the RadioButton style with that. So noone can tell you for sure which properties have you overwritten and which will be the final set of properties.
To understand this change the XAML as following:
<Style BasedOn="{StaticResource {x:Type ToggleButton}}" TargetType="{x:Type ToggleButton}" />
<Style BasedOn="{StaticResource {x:Type ToggleButton}}" TargetType="{x:Type RadioButton}" />
and now see the look & feel of your RadioButton.
I'm not clarifying what is exactly happened and why your ToggleButton is not affected so much.
But as I said it's all about the style hierarchy which is build for a control at last just before rendering on the UI.
I copyied MahApps DataGrid's source code and It works fine if I change directly on the style such as setting HorizontalAlignment from left to right. But if I did
<Style BasedOn="{StaticResource MetroDataGridColumnHeader}" TargetType="{x:Type DataGridColumnHeader}"
x:Key="MetroDataGridColumnHeader1">
<Setter Property="HorizontalAlignment" Value="Right"></Setter>
</Style>
and replace the MetroDataGridColumnHeader with the new MetroDataGridColumnHeader1 inside DataGrid style, It gives strange output. Metro effects disappears.Any ideas?
You should set the HorizontalContentAlignment not the HorizontalAlignment.
<Style BasedOn="{StaticResource MetroDataGridColumnHeader}"
TargetType="{x:Type DataGridColumnHeader}"
x:Key="RightAlignmentMetroDataGridColumnHeader">
<Setter Property="HorizontalContentAlignment"
Value="Right" />
</Style>
Hope this helps.
I have a style defined in a file called MyStyles.xaml:
<Style TargetType="{x:Type igDP:XamDataGrid}">
<Setter Property="FontSize" Value="10" />
<Setter Property="FontFamily" Value="Arial" />
<EventSetter Event="CellUpdating" Handler="grid_CellUpdating"/>
</Style>
In one of my views, I have a XamDataGrid defined:
<igDP:XamDataGrid>
<igDP:XamDataGrid.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/MyProject.TheViews;component/Views/MyStyles.xaml"/>
</ResourceDictionary.MergedDictionaries>
<Style TargetType="{x:Type igDP:XamDataGrid}" BasedOn="{StaticResource {x:Type igDP:XamDataGrid}}">
<Setter Property="FontSize" Value="70"/>
</Style>
</ResourceDictionary>
</igDP:XamDataGrid.Resources>
Basically, I want to keep everything that is defined in the style for XamDatagrid in MyStyles.xaml except for the font size, which I want to be set to 70.
I can't seem to get it to work. With the above, the font is set to 70 but I lose the other settings that are defined in MyStyles (such as the event handling and font family).
What am I doing wrong here?
(Extracting an answer from comments above.)
For overriding the style, I would suggest the following:
Define 2 styles in MyStyles.xaml: a named one which contains the style, and the unnamed one (this will be the default style) simply based on the named one
<Style x:Key="XamDataGridDefaultStyle" TargetType="{x:Type igDP:XamDataGrid}">
<Setter Property="FontSize" Value="10" />
<Setter Property="FontFamily" Value="Arial" />
<EventSetter Event="CellUpdating" Handler="grid_CellUpdating"/>
</Style>
<Style TargetType="{x:Type igDP:XamDataGrid}"
BasedOn="{StaticResource XamDataGridDefaultStyle}"/>
This will define the needed default style for all the views.
For the resources of the view where the customization is needed, define the following override:
<Style TargetType="{x:Type igDP:XamDataGrid}"
BasedOn="{StaticResource XamDataGridDefaultStyle}">
<Setter Property="FontSize" Value="70"/>
</Style>
You'll perhaps need to reference MyStyles.xaml as merged dictionary in the resources of the customized view for StaticResource to work.
My situation is like following.
I have a App.xaml which includes Style for ListView like this:
<Style x:Key="{x:Type ListViewItem}" TargetType="ListViewItem">
<Setter Property="SnapsToDevicePixels" Value="true"/>
<Setter Property="OverridesDefaultStyle" Value="true"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
...
However, I wanna add some styles into another xaml, let's say in Window.xaml like this:
<ListView AlternationCount="2" Background="#FFECECEC">
<ListView.Resources>
<Style x:Key="{x:Type ListViewItem}" TargetType="{x:Type ListViewItem}">
<EventSetter Event="PreviewMouseDoubleClick" Handler="OnPreviewMouseDoubleClick" />
</Style>
</ListView.Resources>
</ListView>
So, what I want to do is define style for base design in App.xaml as Default style.
Then, add some modify such as adding a context menu, adding events from each xaml.
But, with above implementation, Style defined in App.xaml will be overwrote by Style defined in Window.xaml.
Is there any way to solve the issue and achieve it?
Styles have a BasedOn property:
<Style x:Key="Style1">
...
</Style>
<Style x:Key="Style2" BasedOn="{StaticResource Style1}">
...
</Style>
Btw: <Style x:Key="{x:Type ListViewItem}" seems a bit weird. The x:Key should be a unique key in a xaml dictionary - usually a string.
If the BasedOn attribute fits your needs, it's the best choice. For more complicated scenarios though, something like the solution referred to in this question is more flexible.