Set default style to listviewitem - wpf

Everytime listviewitem gets selected, automatically its foreground gets set to white.
How can I prevent this behavior in codebehind?
I tried this, but without effect:
<Trigger Property="IsSelected" Value="False" > or True

It doesn't exactly answer the question, because there is no code behind in this solution. But in my opinion, code behind is not the way to go here.
<Style TargetType="ListBoxItem">
...
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Foreground" Value="Some color"/>
</Trigger>
</Style.Triggers>
</Style>

Related

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 Loses its Row Selection when the focus is lost

I have come accross a very strange behavior with the DataGrid. I have following Trigger on the DataGridRow
<Style TargetType="{x:Type DataGridRow}">
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="{StaticResource SelectionBackgroundBrush}"/>
<Setter Property="Foreground" Value="White"/>
</Trigger>
</Style.Triggers>
</Style>
Initially when the row is selected, I get the behavior from the above trigger. However, after selection, if the DataGrid loses focus (say for example I click some other button on window) the Foreground property loses its value, but the background remains as specified in the trigger. Has anyone ever come accross this behavior, or there is some problem with my code above (or elsewhere in my applicaion for that matter). Any workarounds for the above issue ?
I've used DataGridCell instead of DataGridRow and it works for me
<Style TargetType="{x:Type DataGridCell}">
<Style.Triggers>
<Trigger Property="DataGridCell.IsSelected" Value="True">
<Setter Property="BorderBrush" Value="#CCDAFF" />
<Setter Property="Background" Value="#3399ff"/>
<Setter Property="Foreground" Value="White"/>
</Trigger>
</Style.Triggers>
</Style>
Hope it helps someone!

Style of Controle At various Event

Could anyone tell what would be the style of the Label or Button at various event shows in the below image.
1. MouseOver
2 MousePressed and Control Selected
You can have a look at Style triggers. To figure out what properties you need you can have a look at the UIElement.
For example it can be something like:
<Style x:Key="Triggers" TargetType="Button">
<Style.Triggers>
<Trigger Property="IsPressed" Value="true">
<Setter Property = "Background" Value="LightGray"/>
</Trigger>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property = "BorderBrush" Value="Blue"/>
</Trigger>
<Trigger Property="IsFocused" Value="true">
<Setter Property = "BorderBrush" Value="LightGray"/>
</Trigger>
</Style.Triggers>
</Style>
The above style does not recreate the look of the button from the image, it's just a sample on how it can be done.
You can have a look at the Button class to see what properties you can change.
If I understood your question properly, I think you can change the opacity on MousePressed and Mouseover events.

DataGridCell.IsEditMode?

How Can I know if the DataGridCell is currently in edit mode (not IsSelected), I mean, for example a DataGridTextColumn cell is clicked it becomes a TextBox and not a TextBlock, that's what I call IsEditMode.
I wanna set a trigger-setter for this mode.
EDIT:
I tried to set a general style for DataGridCell.IsEditing but it doesn't seem to do anything.
Here is a snippet of my current code:
<Style TargetType="{x:Type tk:DataGridCell}">
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="{x:Null}"/>
</Trigger>
<Trigger Property="IsEditing" Value="True">
<Setter Property="BorderBrush" Value="#FF62B6CC"/>
<Setter Property="Background" Value="#FFF4F4F4"/>
</Trigger>
</Style.Triggers>
<Setter Property="Foreground" Value="Black"/>
<Setter Property="BorderThickness" Value="0"/>
<Setter Property="BorderThickness" Value="0.5"/>
<Setter Property="BorderBrush" Value="{x:Null}"/>
</Style>
Thanks.
Here's how to do it:
<Trigger Property="IsEditing" Value="True">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="DataGridCell">
<TextBox Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Content.Text, Mode=TwoWay, UpdateSourceTrigger=Default}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" />
</ControlTemplate>
</Setter.Value>
</Setter>
</Trigger>
then style the textbox as you please
If you take a look at DataGridCell.cs file, IsEditing should be good way to find out if cell is in edit mode. But you can't set this property from style, because there is local value assignment in DataGridCell class (which has higher priority from style setter).
So, the answer would be: it should work from trigger, but it will not from the style setter.
Update: Shimmy, it really works. Snoop your application, make sure DataGridCell is using your implicit style. Select DataGridCell in the tree, and check its background property. Every time you go in Edit mode it is updated. But you don't see it, by default, since TextBox doesn't inherit Background property. But that's another story. I think you can tweak CellEditingTemplate to make it working.
The proper way to turn on edit mode is to find the DataGridCell's parent DataGrid and call BeginEdit() on that. If you set it directly, you're sidestepping a lot of DataGrid goo that maintains proper state transitions.

Resources