React on control property in a child element - wpf

I'm creating a WPF UserControl and have defined a dependency property named "Orientation" in it. It's of the type Dock that allows me to set the values Top, Bottom, Left and Right. The UserControl also contains a Border element in its XAML. Depending on the value of the Orientation property, the border shall appear on one side. Here's my triggers I have defined so far:
<Trigger Property="Orientation" Value="Bottom">
<Setter Property="BorderThickness" Value="0,1,0,0"/>
</Trigger>
<Trigger Property="Orientation" Value="Top">
<Setter Property="BorderThickness" Value="0,0,0,1"/>
</Trigger>
<Trigger Property="Orientation" Value="Left">
<Setter Property="BorderThickness" Value="0,0,1,0"/>
</Trigger>
<Trigger Property="Orientation" Value="Right">
<Setter Property="BorderThickness" Value="1,0,0,0"/>
</Trigger>
I've now tried to use these triggers in the Border's style, but they can't find the Orientation property. I've then tried to use it in the UserControl's style, with the TargetName set to my Border, but TargetName cannot be set.
How does the correct XAML code for this look like?

In your Border Style use DataTrigger instead of Trigger e.g
<DataTrigger Binding="{Binding Orientation, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}" Value="Bottom">
<Setter Property="BorderThickness" Value="0,1,0,0"/>
</DataTrigger>
similarly update all the triggers

Related

Repeat Button Style Setter/Triggers Doesn't Work

I'm using the Extended WPF Toolkit's IntegerUpDown control.
I was able to stylize the arrow buttons in a previous question.
Before
After
I've run into some additional style problems:
I'm using PresentationFramework.Aero and Aero2.
<!-- RepeatButton Style -->
<Style x:Key="{x:Static theme:ResourceKeys.SpinnerButtonStyleKey}" TargetType="RepeatButton">
<Style.Triggers>
<Trigger Property="IsEnabled" Value="True">
<Setter Property="Background" Value="Transparent" />
<Setter Property="BorderBrush" Value="Transparent" />
</Trigger>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Background" Value="Transparent"/>
<Setter Property="BorderBrush" Value="Transparent" />
</Trigger>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="DarkBlue"/>
<Setter Property="BorderBrush" Value="Transparent" />
</Trigger>
</Style.Triggers>
</Style>
Link to the full XAML: http://pastebin.com/ETYgHEpz
The IsEnabled False Trigger will not override the Disabled Background or Border Color. It always stays White, I need it Transparent. However I can override the Opacity and BorderThickness. (Windows 10):
Cannot change the IsMouseOver Background Color, always Light Blue. (Windows 10):
Windows 7 always displays a White Border, even if BorderThickeness is 0 and Color is Transparent or {x:Null}:
I think it is being overriden by a Control Template, but IntergerUpDown doesn't have a RepeatButton Style within the Control Template and I've had trouble trying to add one. I had to create the Style outside.
some properties won't work when you set via Style setters. This is because of Dependency Property Value Precedence. Try changing the values in ControlTemplate triggers.

RowStyle in DataGrid working only partially

This is weird, so I'm sure I'm missing something (almost) obvious here. I wish to control the appearance of the selected row and cell so I'm playing with the triggers. The triggering seems to work, because I see the settings I've maid with regarding the colors and thickness.
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="Aquamarine"></Setter>
</Trigger>
</Style.Triggers>
</Style>
</DataGrid.RowStyle>
<DataGrid.CellStyle>
<Style TargetType="DataGridCell">
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="BorderThickness" Value="0"></Setter>
</Trigger>
</Style.Triggers>
</Style>
</DataGrid.CellStyle>
However, the problems is that the rows' background is colored in the default blue on all the columns containing data and in my custom aquamarine on the empty space to the right of the last column, just like the image below depicts.
What gives? My google-fu haven't shown anything about persistent defaults needed to be removed...
The default blue coloring is part of the DataGridCell's Template.
You can retemplate the DataGridCell and omit this functionality, or you can instead just add a setter in your existing DataGridCell style's IsSelected trigger and set the Background to Transparent.
<DataGrid.CellStyle>
<Style TargetType="DataGridCell">
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="BorderThickness" Value="0"/>
<Setter Property="Background" Value="Transparent"/>
</Trigger>
</Style.Triggers>
</Style>
</DataGrid.CellStyle>

Background color of selected DataGridRow not working / how to combine with AlternationIndex?

I'm trying to style a DataGrid, this includes styling DataGridRow as well. What I want is having an alternating background behavior on my rows (say, White/Grey); when a row is selected, the background should be YellowGreen. Somehow the following code is not working, but I'm sure using DataGridCell for the selection part would work; I'd just like to know why this isn't doing it:
<Style TargetType="{x:Type DataGridRow}">
<Style.Triggers>
<Trigger Property="DataGridRow.AlternationIndex" Value="0">
<Setter Property="Background" Value="{StaticResource EnBWLichtgrau}"/>
</Trigger>
<Trigger Property="DataGridRow.AlternationIndex" Value="1">
<Setter Property="Background" Value="White"/>
</Trigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="DataGridRow.AlternationIndex" Value="0"/>
<Condition Property="DataGridRow.IsSelected" Value="True"/>
</MultiTrigger.Conditions>
<MultiTrigger.Setters>
<Setter Property="Foreground" Value="Red"/>
<Setter Property="Background" Value="Pink"/>
</MultiTrigger.Setters>
</MultiTrigger>
</Style.Triggers>
</Style>
<Style TargetType="{x:Type DataGrid}">
<Setter Property="AlternationCount" Value="2"/>
<Setter Property="BorderBrush" Value="{StaticResource EnBWAnthrazitgrau}"/>
<Setter Property="HeadersVisibility" Value="Column"/>
</Style>
Maybe some setters override the MultiTrigger setters, maybe something else, I don't know. The MultiTrigger isn't really necessary right now, but even a simple Trigger on IsSelected doesn't work this way.
As I said, I could probably go over to styling DataGridCell, but the "why" is what I'm interested in :-) Thanks!
EDIT: I tried using DataGridCell instead for the IsSelected part and it works as expected. But what if I wanted the selected row background color to be different depending on the AlternationIndex of the DataGridRow the cell is in? Is there some way to get the value of this index inside the DataGridCell style definition e.g.?

WPF DataGrid RowStyle for selected row not changing the background and foreground color

I am using Visual Studio 2012 on windows 7. I need to know why the following style for Grid's selected row does not work for background and foreground colors but works perfectly fine for other properties like BorderBrush and BorderThickness etc? Though I can see them changing while mouse over grid rows.
<Style x:Key="gridRowStyle" TargetType="{x:Type DataGridRow}">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="PeachPuff"/>
<Setter Property="Foreground" Value="BlueViolet"/>
</Trigger>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="PeachPuff"/>
<Setter Property="Foreground" Value="BlueViolet"/>
<Setter Property="BorderBrush" Value="BlueViolet" />
<Setter Property="BorderThickness" Value="2" />
</Trigger>
</Style.Triggers>
</Style>
Here is how I am using on grid.
<DataGrid RowStyle="{StaticResource gridRowStyle}">
I am stressing on to know "why" rather than solution to the problem as I already have solution to problem if I use grid cell style instead of rowstyle like following:
<Style x:Key="gridCellStyle" TargetType="{x:Type DataGridCell}">
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="PeachPuff"/>
<Setter Property="Foreground" Value="BlueViolet"/>
</Trigger>
</Style.Triggers>
</Style>
In the Default style of the DataGridCell having the following default style trigger.
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}"/>
<Setter Property="BorderBrush" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
</Trigger>
So if you written trigger for DataGridRow, then it will only apply to the element that was placed before the DataGridCell in visual tree.
So to change the Background & Foreground while selection, you must write the trigger in DataGridCell style or remove the default trigger from the style.
Simply remove this attributs at row level in the datagrid, they have priority over the trigged properties.
nezac

How to determine index of current ListBox item from DataTemplate?

I have a ListBox. Now I want to write a DataTemplate in such way, that the first item will have red background and white background for other items. I guess I need to write a DataTrigger, but I have no idea how do determine that DataTemplate is applying to the first item.
items controls have an alternation count that you use to style against
have a look here :
<Style TargetType="{x:Type ListBoxItem}">
<Style.Triggers>
<Trigger Property="ItemsControl.AlternationIndex" Value="0">
<Setter Property="Background" Value="LightBlue"></Setter>
</Trigger>
<Trigger Property="ItemsControl.AlternationIndex" Value="1">
<Setter Property="Background" Value="LightGreen"></Setter>
</Trigger>
</Style.Triggers>
</Style>
enjoy!

Resources