WPF TabControl - How to remove Margin above inactive Tabs? - wpf

Any Ideas how to remove the Margin in the following image?

Checked it via Snoop. By default, the selected TabItem has a margin of -2,-2,-2,-1, so you can set this top margin for all tab items:
<Style TargetType="{x:Type TabItem}" BasedOn="{StaticResource {x:Type TabItem}}">
<Setter Property="Margin" Value="0,-2,0,0"/>
</Style>

Related

How to change the background of a MaterialDesign ListBoxItem in XAML? (WPF)

I am trying to change the background color of the MaterialDesign ListBoxItem style but I can't get it to work.
I know about overriding the style like this :
<Style x:Key="MaterialDesignListBoxItem"
TargetType="ListBoxItem"
BasedOn="{StaticResource MaterialDesignListBoxItem}">
</Style>
But I won't get the MaterialDesign animations as it will just be a regular button.
Is there any way to change the background color and keep the MaterialDesign look and feel?
Set the ItemContainerStyle property of the ListBox to a custom Style that is based on MaterialDesignListBoxItem, e.g.:
<ListBox>
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem" BasedOn="{StaticResource MaterialDesignListBoxItem}">
<Setter Property="Background" Value="Green" />
</Style>
</ListBox.ItemContainerStyle>
</ListBox>

DataPicker Button influenced by a global styling on all Buttons

I'm maintaining a big old C#/WPF application in which a style has been set globally for all buttons It can sound ugly, but I can't change this without refactoring the whole app.
Here is an extract of this style:
<Style TargetType="Button">
<Setter Property="Height" Value="32"/>
<Setter Property="MinWidth" Value="96"/>
<Setter Property="Margin" Value="10"/>
</Style>
The problem is that when I want to use a DatePicker, this global style influenced the appearance of the DataPicker:
Is there a simple way to restore the default Margin, Height and MinWidth only for the Button inside the DatePicker?
You can override the Style for the Buttons locally. The following XAML sets the Style for all Buttons inside the DatePicker back to the default Style.
<DatePicker>
<DatePicker.Resources>
<!-- Default Style -->
<Style TargetType="{x:Type Button}"/>
</DatePicker.Resources>
</DatePicker>
Edit
As requested in the comments, a Style to fix this issue globally
<Style TargetType="{x:Type DatePicker}">
<Style.Resources>
<Style TargetType="{x:Type Button}"/>
</Style.Resources>
</Style>
Note: This Style should be placed in the same hierarchy-context as the Button Style.

Override item Style Template with a DataTemplateSelector

I've got a Style on ListViewItem that sets the Theme property
<Style x:Key="{x:Type ListViewItem}" TargetType="{x:Type ListViewItem}">
<Setter Property="Template" Value="{StaticResource ListViewItemTemplate}"/>
</Style>
However, in one of the cases where I'm using a ListView I want to use a DataTemplateSelector to determine which Template to use
<Style x:Key="MyListStyle" TargetType="{x:Type ListView}" BasedOn="{StaticResource {x:Type ListView}}">
<Setter Property="ItemTemplateSelector" Value="{StaticResource MyItemTemplateSelector}"/>
</Style>
It's applied like this
<ListView Style="{StaticResource MyListStyle}/>
However, it would appear that the Style on the item takes over and that style is applied to all the items in the ListView. I found this question which had a similar problem, however, the solution simply doesn't use the Template on the Item style at all. I need to keep that.
I've played around with the ContentTemplateSelector by restyling the ListViewItems in that ListView
<Style x:Key="MyItemStyle" BasedOn="{StaticResource {x:Type ListViewItem}}" TargetType="{x:Type ListViewItem}">
<Setter Property="ContentTemplateSelector" Value="{StaticResource MyItemTemplateSelector}"/>
</Style>
However, the Template on the other style is used instead. If I try nulling the Template then nothing shows up at all!
<Setter Property="Template" Value="{x:Null}"/
Is there a way that I can replace the Template on the ListViewItems for a given ListView while keeping the rest of my Style?
I was unable to override the Template set on the ListViewItem in the ListView Style.
To get around this I replaced the Template on the ListViewItem with another ItemTemplateSelector in my base ListView Style. This selector always returns my original Template. In my ListView child Style the new ItemTemplateSelector is used.

MahApps setting style based on existing style has strange output

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.

Images direction changed because FlowDirection is set to RightToLeft

I have a WPF UserControl in which I've defined FlowDirection="RightToLeft"
I've noticed that the direction of all button images in the toolbar has changed because of that.
How can I avoid that without setting FlowDirection="LeftToRight" for each button ?
You can declare default Style in your resource for button so that it gets applied to all button by default in your UserControl. This way you don't have to go each button and set its property manually.
<UserControl.Resources>
<Style TargetType="Button">
<Setter Property="FlowDirection" Value="LeftToRight"/>
</Style>
</UserControl.Resources>
It won't work for button since buttons in toolbar apply style identified by ToolBar.ButtonStyleKey. So add you need to have two styles and simply set second style to be based on first button. This is how you will do that:
<UserControl.Resources>
<Style TargetType="Button">
<Setter Property="FlowDirection" Value="LeftToRight"/>
</Style>
<Style x:Key="{x:Static ToolBar.ButtonStyleKey}" TargetType="Button"
BasedOn="{StaticResource {x:Type Button}}"/>
</UserControl.Resources>

Resources