Hopefully someone can help here.
I have a ListView that is populated by a List Property in my ViewModel.
I have another List Property in my ViewModel that contains the Rows in the UI I need highlighting.
<ListView.Resources>
<Style TargetType="ListViewItem">
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Foreground" Value="Red"/>
</Trigger>
</Style.Triggers>
</Style>
</ListView.Resources>
This does exactly what is looks like, any item I select in the ListView highlights in Red.
I want to be able to Bind this style trigger to the List property in my ViewModel.
Anybody know how this can be achieved?
Create a property called IsSelected on your item and bind it to your ListViewItem.IsSelected
<Style TargetType="ListViewItem">
<Setter Property="IsSelected" Value="{Binding IsSelected}" />
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Foreground" Value="Red"/>
</Trigger>
</Style.Triggers>
</Style>
Edit
If you want to apply other style setters based on a property in your DataItem, use a DataTrigger instead of a regular Trigger. Regular Triggers are only meant to be used on UI Element properties, not bindings.
<Style TargetType="ListViewItem">
<Style.Triggers>
<DataTrigger Binding="{Binding IsHighlighted}" Value="True">
<Setter Property="Foreground" Value="Red" />
</DataTrigger>
</Style.Triggers>
</Style>
Related
I have made a table using a DataGrid with List as its ItemsSource binding. I currently want to change the row styling of my 'Calculation Functions' tab ONLY. I want to be able to set it so that any row in 'Calculation Functions' has the Foreground and Background as Red and all my other tabs remain normal. How would this be possible?
<DataGrid Name="ReflectionDataGrid"SelectedItem="{Binding SelectedItem}" ItemsSource="{Binding List}" AutoGenerateColumns="False" IsHitTestVisible="True">
<DataGrid.RowStyle>
<Style TargetType="{x:Type DataGridRow}">
<Style.Triggers>
<Trigger Property="Header" Value="Calculation Functions">
<Setter Property="Background" Value="Red"/>
<Setter Property="Foreground" Value="Red"/>
</Trigger>
</Style.Triggers>
</Style>
</DataGrid.RowStyle>
</DataGrid>
You could bind to the Header property of the SelectedItem of the parent TabControl:
<Style TargetType="{x:Type DataGridRow}">
<Style.Triggers>
<DataTrigger Binding="{Binding SelectedItem.Header, RelativeSource={RelativeSource AncestorType=TabControl}}"
Value="Calculation Functions">
<Setter Property="Background" Value="Red"/>
<Setter Property="Foreground" Value="Red"/>
</DataTrigger>
</Style.Triggers>
</Style>
I need to hide rows under a certain condition, I do this through a style trigger. But for some reason it doesn't work if you use Map apps styles. If I delete styles from the dictionary, everything works. What i need to do to make it work with Mah app styles as well?
<DataGrid.ItemContainerStyle>
<Style TargetType="DataGridRow">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=IsArchive}" Value="True">
<Setter Property="Visibility" Value="Collapsed" />
</DataTrigger>
</Style.Triggers>
</Style>
</DataGrid.ItemContainerStyle>
I also tried using baseOn but it still doesn't work
Set the RowStyle instead of ItemContainerStyle:
<DataGrid.RowStyle>
<Style TargetType="DataGridRow" BasedOn="{StaticResource MahApps.Styles.DataGridRow}">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=IsArchive}" Value="True">
<Setter Property="Visibility" Value="Collapsed" />
</DataTrigger>
</Style.Triggers>
</Style>
</DataGrid.RowStyle>
I'm trying to set a trigger to hide my window whenever a property in my ViewModel changes. I tried using the same trigger on a different element and that worked exactly as expected, so it appears that I cannot change the visibility property of a window using a trigger. Are there any workarounds I can use to hide my window?
<Window.Style>
<Style TargetType="Window" BasedOn="{StaticResource Shell}">
<Setter Property="Visibility" Value="Hidden"/>
<Setter Property="Left" Value="{Binding SecondDisplayLeftSide}"/>
<Style.Triggers>
<DataTrigger Binding="{Binding ShellType}" Value="InstructorMultiDisplay">
<Setter Property="Visibility" Value="Visible"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Window.Style>
I've implemented code to get around the double click issue when selecting a row in a WPF DataGrid. I'm using the following code from here: https://stackoverflow.com/a/5857908/40106.
<Style TargetType="DataGridCell">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="IsEditing" Value="True" />
</Trigger>
</Style.Triggers>
</Style>
Rows have alternating colors. The problem is when I mouse over a row, in one column, the light blue color is replaced by white.
The above code works great except for this one issue. How do I stop the color from changing when mousing over a row?
I have tried the following but it doesn't have any effect:
<Style TargetType="DataGridCell">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="IsEditing" Value="True" />
<Setter Property="Background" Value"AliceBlue" />
</Trigger>
</Style.Triggers>
</Style>
The problem is, that the cell will display it's edit style when you hover the mouse above the cell.
For a DataGridTextColumn this means, that a TextBox with a white background is displayed.
You can set a Style to <DataGridTextColumn.EditingElementStyle> and set the Background to transparent.
<DataGridTextColumn Header="Name" Binding="{Binding Name}" >
<DataGridTextColumn.EditingElementStyle>
<Style TargetType="TextBox">
<Setter Property="Background" Value="Transparent"></Setter>
<Setter Property="BorderThickness" Value="0"></Setter>
</Style>
</DataGridTextColumn.EditingElementStyle>
</DataGridTextColumn>
To get the white Background, when actually editing the cell you could add another trigger to the IsSelected event:
<Style TargetType="DataGridCell">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="IsEditing" Value="True" />
</Trigger>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="White"/>
</Trigger>
</Style.Triggers>
</Style>
Another option would be to apply the DataGridCell Style to CheckBoxColumns only. For other Column types it wouldn't make a difference anyway.
I am trying to add a context menu to my WPF datagrid is specific to each row, because its items need to depend on the DataContext for that row. For example "Save" might be disabled if I know the row has not been edited.
I am looking at the accepted answer for Create contextmenus for datagrid rows and trying to adapt it to the existing xaml I am working with, but I don't know how to use this solution on top of my existing RowStyle.
If I copy and paste the context menu code everything works, but I already have this for RowStyle:
<DataGrid.RowStyle>
<Style TargetType="{x:Type DataGridRow}">
<Style.Triggers>
<Trigger Property="AlternationIndex" Value="1">
<Setter Property="Background" Value="#eed3f7" />
</Trigger>
[...]
</Style.Triggers>
</Style>
</DataGrid.RowStyle>
And I don't understand how to incorporate:
<DataGrid RowStyle="{StaticResource DefaultRowStyle}"/>
Please help!
I'm guessing that you've been looking at this for so long that you can't see the wood for the trees. In your linked answer, there is this Style:
<Style x:Key="DefaultRowStyle" TargetType="{x:Type DataGridRow}">
<Setter Property="ContextMenu" Value="{StaticResource RowMenu}" />
</Style>
You say that you can't use it because you have your own Style that you want to apply... but the Style above has only one Setter and so there is nothing stopping you from copying that one Setter into your Style:
<DataGrid.RowStyle>
<Style TargetType="{x:Type DataGridRow}">
<Setter Property="ContextMenu" Value="{StaticResource RowMenu}" /><!--<<<<<<<<-->
<Style.Triggers>
<Trigger Property="AlternationIndex" Value="1">
<Setter Property="Background" Value="#eed3f7" />
</Trigger>
[...]
</Style.Triggers>
</Style>
</DataGrid.RowStyle>
Alternatively, you could have based your Style on the DefaultRowStyle:
<DataGrid.RowStyle>
<Style TargetType="{x:Type DataGridRow}" BasedOn="DefaultRowStyle">
<Style.Triggers>
<Trigger Property="AlternationIndex" Value="1">
<Setter Property="Background" Value="#eed3f7" />
</Trigger>
[...]
</Style.Triggers>
</Style>
</DataGrid.RowStyle>