WPF Datagrid highlight row and column - c#

Is there a way to programmatically highlight a row and column in the WPF datagrid? I am using the scrollIntoView method to jump to that row and column. I'd like to highlight that row as well to highlight to the user that this is what's important. Thanks!

If you have a business object for whatever is represented in the datagrid, in the DataGrid.RowStyle I would use a DataTrigger bound to a boolean in the object representing the row. Then when you ScrollIntoView you could set this boolean and let XAML handle setting the row color for you. It might get weird because then you would have to reset any other ones set. But I think this could be an easy solution.
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=IsSelected}" Value="True">
<Setter Property="Background" Value="Red" />
</DataTrigger >
</Style.Triggers>
</Style>
</DataGrid.RowStyle>

Related

Wpf datagrid : Add button on selected Cell after binding and set background on specific empty cells

I've got a DataGrid into a WPF window.
On loaded the window, my datagrid is empty.
After selected the Excel file, I've got a function to fill my datagrid.I create a new DataTable, adding my columns and fill my data. Finnaly, I do a my_dataGrid.DataContext = dataTable;
Everything is ok.
Now, I'd like to do 2 things :
For a spectific column, I'd like to set the Background color of empty cells. I found this code to color all empty cells but not for a spectific column...
<DataGrid.CellStyle>
<Style TargetType="DataGridCell" >
<Style.Triggers>
<DataTrigger Binding="{Binding Path=Content.Text, RelativeSource={RelativeSource Self}}" Value="" >
<Setter Property="Background" Value="Gray"/>
</DataTrigger>
</Style.Triggers>
</Style>
</DataGrid.CellStyle>
When I click on cell of a specific column, I'd like to show a button, I don't know how
Could you help me please?
Here I my phases on captures...

How to change the colour of a selected WPF DataGrid row header

I have a WPF DataGrid whose row headers get filled with (a small amount of) text, and this works great... until you highlight the row, when the text turns white and is barely visible:
In an attempt to force it to stay black, I set the following code containing both an IsSelected trigger and an explicit DataTemplate with its colour also set.
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Foreground" Value="Black"/>
</Trigger>
</Style.Triggers>
<Setter Property="HeaderTemplate">
<Setter.Value>
<DataTemplate>
<TextBlock Foreground="Black" Text="{Binding}" />
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
</DataGrid.RowStyle>
Unfortunately, neither of these has any effect. The text remains stubbornly white until the row is deselected.
How can I force this text to remain black (or any other colour) when selected?
The style trigger is defined on the row itself instead of the header's template item. Simply move the trigger definition into the header template would do the trick.

DataGrid SelectedIndex set 0 by default

I am using WPF to make a program with DataGrid. I have a Delete button and when I select one row in DataGrid, the button enables and I can use it. So if SelectedIndex is bigger than -1 the button enables. But I found out, the SelectedIndex is set to 0 by default, so it's like the first row is always selected.
Is there any other property of DataGrid I could use? Or is there some other way to make this work?
You can use DataTrigger on button to handle the disable/visible of buttons, something like :
<Button.Style>
<Style>
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=nameOfDataGrid, Path=SelectedItem}" Value="{x:Null}">
<Setter Property="IsEnabled"Value="False"></Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</Button.Style>

Remove formatting for specific column RadGrid MVVM

I am using a WPF Application, In that i am using a telerik gird and also i am using MVVM model to bind data in the grid. I have set the rows to gray color and readonly based on some values. Its working fine.
Now i have to remove the formatting for 3 specific columns (i.e) i have to make the background to white and readonly to false.
The row made gray using the below code,
//Code:
<telerik:RadGridView.RowStyle>
<Style TargetType="{x:Type telerik:GridViewRow}">
<Style.Triggers>
<DataTrigger Binding="{Binding IsActive}" Value="True">
<Setter Property="Background" Value="Gray" />
</DataTrigger>
</Style.Triggers>
</Style>
</telerik:RadGridView.RowStyle>
Row made ReadOnly using the below code,
<telerik:RadGridView x:Name="radGridView" IsReadOnlyBinding="{Binding IsActive}" />
How can i achieve this?
I have achieved like this,
<telerik:GridViewDataColumn Header="Name" IsReadOnlyBinding="{Binding IsActive}">
</telerik:GridViewDataColumn>
Binding the value to each of the column made this work. For those columns which are editable just got rid of IsActive.

Binding WPF Datagrid cell background colour with trigger

I want the background colour of a WPF datagrid cell to change colour when the contents have been modified. Each cell has behind it a ViewModel object which contains the following properties - Value, OriginalValue and Modified. When the user edits the cell contents, this automatically triggers the Amount property via data binding. This property setter then checks it against the original value and sets the boolean Modified property to true or false respectively, notifies the bindings for those properties to update.
I have so far achieved a partial result with a Style on the ElementStyle property of the DataGridTextColumn as follows
<Style x:Key="DataGridTextStyle" TargetType="{x:Type TextBlock}">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=MyViewModel.Modified}" Value="True">
<Setter Property="Background" Value="Yellow"/>
</DataTrigger>
</Style.Triggers>
</Style>
This updates the text content background colour, but that is only a small area in the center of the cell. I want the entire cell to updates it's background colour, not just the textblock attribute.
Can I modify the above trigger to search upwards in the visual tree to find a parent DataGridCell and set the Background property on that, rather than setting the background colour of the current textblock only?
You need to set CellStyle to target DataGridCell instead of only TextBlock.
If you want this dataTrigger to be applied for all cells in your dataGrid, set style on DataGrid CellStyle otherwise you can do that on specific DataGridTextColumn CellStyle as well.
DataGrid
<DataGrid>
<DataGrid.CellStyle>
<Style TargetType="DataGridCell">
<Style.Triggers>
<DataTrigger Binding="{Binding MyViewModel.Modified}"
Value="True">
<Setter Property="Background" Value="Yellow"/>
</DataTrigger>
</Style.Triggers>
</Style>
</DataGrid.CellStyle>
</DataGrid>
DataGridTextColumn
<DataGrid>
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Name}">
<DataGridTextColumn.CellStyle>
<Style TargetType="DataGridCell">
<Style.Triggers>
<DataTrigger Binding="{Binding MyViewModel.Modified}"
Value="True">
<Setter Property="Background" Value="Yellow"/>
</DataTrigger>
</Style.Triggers>
</Style>
</DataGridTextColumn.CellStyle>
</DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
Others May benefit from this WPF "Dynamic Data Triggers" in code behind method
This code allows users to highlight data rows with the specified text they want.
var st = new Style();
st.TargetType = typeof(DataGridRow);
var RedSetter = new Setter( DataGridRow.BackgroundProperty, Brushes.Red);
var dt = new DataTrigger(){
Value = CurrentTextToFilter,
Binding = new Binding("Value")
};
dt.Setters.Add(RedSetter);
st.Triggers.Add(dt);
XDG.RowStyle = st;
PropChanged("MainDataCollection");
The parm CurrentTextToFilter the text the user entered into a XAML Textbox.Text property that was bound to the code behind.
The variable XDG is the datagrid XAML name and the RowStyle is set to the new style.
Make sure to add the setter to the DataTrigger as shown. If you add it directly to the Rowstyle, all the rows will turn red.

Categories