Remove formatting for specific column RadGrid MVVM - c#

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.

Related

Show a Column Sorted by default in a DataGrid WPF XAML

I'm trying to have a column sorted by default in a DataGrid in XAML.
I've tried several approaches but none of them work.
The last one I've tried it's this, it works the first time I run the program but then it's overruled somehow.
<DataGridTextColumn Header="Bar"
Width="50"
CanUserSort="True"
HeaderStyle="{StaticResource CenterGridHeaderStyle}"
Binding="{BindingBarName,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
SortDirection="Ascending">
<DataGridTextColumn.CellStyle>
<Style TargetType="DataGridCell">
<Setter Property="TextBlock.TextAlignment" Value="Center" />
</Style>
</DataGridTextColumn.CellStyle>
</DataGridTextColumn>
Any idea?
Thanks
Setting the DataGridColumn.SortDirection doesn't actually sort the column. It's just used to display the arrow indicator in the header.
To actually sort the items, you should also add a SortDescription to the DataGrid:
dataGrid1.Items.SortDescriptions.Add(new SortDescription("BarName", ListSortDirection.Ascending));

WPF XceedDataGrid use datatrigger to collapse/Hide rows

I am using the DataGridControl from the xceeddatagrid library(http://schemas.xceed.com/wpf/xaml/datagrid). What I am trying to do is hide/Collapse the datarow based on a property from the class model. I am trying to use a datatrigger. My issue is that the DataGrid will remove the data from the datarow but not the physical space of the row. So it leaves a blank row. I tried a regular out of the box .net datagrid and I have no issue. I stripped away all styles and still have this issue.
xmlns:xcdg="http://schemas.xceed.com/wpf/xaml/datagrid"
<xcdg:DataGridControl x:Name="dgr"
ItemsSource="{Binding Path=.Data}"
AutoCreateColumns="False">
<xcdg:DataGridControl.Resources>
<Style TargetType="{x:Type xcdg:DataRow}">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=.IsTrue}" Value="true">
<Setter Property="Visibility" Value="Collapsed" />
<Setter Property="Visibility" Value="Hidden" />
</DataTrigger>
</Style.Triggers>
</Style>
</xcdg:DataGridControl.Resources>
<xcdg:DataGridControl.Columns>
<xcdg:Column FieldName="IsToday" Title="Today" Width="40" />
<xcdg:Column FieldName="Name" Title="Name" Width="70" />
<xcdg:Column FieldName="Address" Title="Address" Width="40" />
</xcdg:DataGridControl.Columns>
</xcdg:DataGridControl>
Check out this doc page about the Visibility Enum. It would seem the value you need is Collapsed:
Do not display the element, and do not reserve space for it in layout.
I'm not sure that you need the "." in the Path of the DataTrigger Binding. I believe that each DataRow's IsTrue property will be accessed implicitly without the dot.
Besides that, the main issue I see is that there are two setters for the same property within the DataTrigger. Based on your description, it would seem that the DataRow's Visibility is being set to Collapsed, but then to Hidden, which does exactly what you describe as the problem:
Do not display the element, but reserve space for the element in layout.
So, I believe the solution is to remove the conflicting Visiblity Setter.

WPF DataGrid cell style from different property in XAML

Is it possible to bind a data grid column value to one property of the bound class specified in ItemsSource, but reference a different property in that class to style the cell?
Here is a code example:
<DataGrid ItemsSource="MyCollection">
<DataGridTextColumn Header="MyColumn" Binding={Binding ColumnText} />
<DataGridTextColumn Header="MyColumn2" Binding={Binding ColumnText2} />
</DataGrid>
Suppose that I have a two further properties (Enum or Brush) in the objects in MyCollection that I want to refer to to set the background colour of the respective cells; can this be done?
EDIT - I need to do this for multiple columns, each looking at a different property to ascertain the colour it should be; I have added a second column to the code example above.
I know I can set the style based on the value in ColumnText, but that is not what I need to do.
I have tried setting a style data trigger, but when I try to bind, I can only bind to something from the overall data context, not to another property within the object that is populating the current row.
Many thanks!
If I understand correctly, you are trying to set a cell background by a property from the Row model.
You can achieve this by setting a cell style, and set a DataTrigger to that style to bind to a property you want.
Example
You want to make each cell, that has the number 3 to be painted green:
<DataGrid ItemsSource="{Binding Rows}" AutoGenerateColumns="True">
<DataGrid.CellStyle>
<Style TargetType="{x:Type DataGridCell}">
<Style.Triggers>
<DataTrigger Binding="{Binding Number}" Value="3">
<Setter Property="Background">
<Setter.Value>
<SolidColorBrush Color="Green"/>
</Setter.Value>
</Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</DataGrid.CellStyle>
</DataGrid>
And, of course, you can change the Number property with your own property, and the value you are interested in.
If you want to do something more complex, like range of values and such, you should go with the conventional converters way.
Edit
If you want to make a different cell style for each column, you should explicitly set columns:
<DataGrid ItemsSource="{Binding Rows}" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Number}">
<DataGridTextColumn.CellStyle>
<Style TargetType="{x:Type DataGridCell}">
<Style.Triggers>
<DataTrigger Binding="{Binding Number}" Value="3">
<Setter Property="Background">
<Setter.Value>
<SolidColorBrush Color="Green"/>
</Setter.Value>
</Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</DataGridTextColumn.CellStyle>
</DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
One important thing to notice though,
You have to set AutoGenerateColumns="False" or else the columns will generate twice.

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.

WPF Datagrid highlight row and column

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>

Categories