This is a migrated thread and some comments may be shown as answers.

Setting row color based on cell value data trigger doesn't work

6 Answers 544 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Beckie
Top achievements
Rank 1
Beckie asked on 13 May 2010, 01:31 AM
Setting row color based on cell value data trigger doesn't work with custom theme/style.

   We used to be able to have this Data Trigger work and it still does if we were to use any of the pre-packaged themes.  However we have developed our theme/style and now this data trigger no longer works for setting a row's background color.  We are using alternate row color so I fail to see how to apply the possible Converter strategy as an alternate solution that I've seen in other posts.

 

 

<telerik:RadGridView Height="200" Width="545" Name="gvNoteList" Grid.Row="1" Grid.Column="1" RowStyle="{StaticResource GridViewRowStyle}" AlternateRowStyle="{StaticResource GridViewAlternateRowStyle}" Style="{StaticResource GridReadOnly}" SelectionChanged="gvNoteList_SelectionChanged">

 

 

 

<telerik:RadGridView.Resources>

 

 

 

<Style TargetType="{x:Type telerik:GridViewRow}">

 

 

 

<Style.Triggers>

 

 

 

<DataTrigger Binding="{Binding Path=IsSticky}" Value="True">

 

 

 

<DataTrigger.Setters>

 

 

 

<Setter Property="Background" Value="#FFFF80" />

 

 

 

<Setter Property="Foreground" Value="#000000" />

 

 

 

<Setter Property="FontSize" Value="12" />

 

 

 

</DataTrigger.Setters>

 

 

 

</DataTrigger>

 

 

 

</Style.Triggers>

 

 

 

</Style>

 

 

 

</telerik:RadGridView.Resources>

 

 

 

</telerik:RadGridView>

 

6 Answers, 1 is accepted

Sort by
0
Yavor Georgiev
Telerik team
answered on 13 May 2010, 10:16 AM
Hello Curt,

 
You can achieve the desired effect by having an AlternateRowStyle based on your own custom RowStyle that defines your DataTriggers like so:

<Grid.Resources>
        <Style TargetType="{x:Type telerik:GridViewRow}" x:Key="GridRowStyle">
            <Style.Triggers>
                <DataTrigger Binding="{Binding Title}" Value="Mr.">
                    <DataTrigger.Setters>
                        <Setter Property="Background" Value="#FF6394CE" />
                    </DataTrigger.Setters>
                </DataTrigger>
                <DataTrigger Binding="{Binding Title}" Value="Ms.">
                    <DataTrigger.Setters>
                        <Setter Property="Background" Value="#FFE26DB4" />
                    </DataTrigger.Setters>
                </DataTrigger>
            </Style.Triggers>
        </Style>
        <Style x:Key="GridAlternateRowStyle" BasedOn="{StaticResource GridRowStyle}" TargetType="{x:Type telerik:GridViewRow}">
            <Setter Property="Background" Value="Gray" />
        </Style>
</Grid.Resources>

and then just reference them from your RadGridView:
<telerik:RadGridView ItemsSource="{Binding Customers}" x:Name="radGrid" RowStyle="{StaticResource GridRowStyle}" AlternateRowStyle="{StaticResource GridAlternateRowStyle}" AlternationCount="2">
This way your AlternateRowStyle inherits the DataTriggers from your custom RowStyle.

Best wishes,
Yavor Georgiev
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
K
Top achievements
Rank 1
answered on 13 Mar 2014, 06:48 PM
Hi
How do I set the edited cell foreground color to blue dynamically until I submit the form in WPF? I am using Telerik.RadGridView.

here is what I tried:

<telerik:GridViewDataColumn Header="New Value" DataMemberBinding="{Binding Path=NewLR}" TextWrapping="Wrap" IsReadOnly="False">
<telerik:GridViewDataColumn.CellStyle>
<Style TargetType="telerik:GridViewCell">
<Style.Triggers>
<DataTrigger Binding="{Binding HasChanged}" Value="True">
<Setter Property="FontWeight" Value="Bold" />
<Setter Property="Foreground" Value="Blue" />
</DataTrigger>
</Style.Triggers>
</Style>
</telerik:GridViewDataColumn.CellStyle>
</telerik:GridViewDataColumn>
0
Yoan
Telerik team
answered on 17 Mar 2014, 05:20 PM
Hi,

I am sending you a sample project, which demonstrates how to achieve the desired functionality. Basically, you need to mark a cell as edited. For this reason, you can have a boolean property of your business object which will hold this information. Then you can change it when CellEditEnded event of RadGridView fires. 
 

Regards,
Yoan
Telerik
 

DevCraft Q1'14 is here! Watch the online conference to see how this release solves your top-5 .NET challenges. Watch on demand now.

 
0
K
Top achievements
Rank 1
answered on 18 Mar 2014, 01:52 PM
Thanks Yoan for a quick response.  But, I seem to have an issue as the Style gets lost when I move to next cell.  I have three editable cells that I need to change the foreground color when the value changes. I need to maintain the style on all changed columns until I click the button. here is my code for your review...

in xaml:

<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="../../Resources/StyleResources.xaml" />
</ResourceDictionary.MergedDictionaries>
<converters:StringToVisibilityConverter x:Key="StringToVisibilityConverter"/>
<converters:BooleanToColorConverter x:Key="BooleanToColorConverter"/>
<converters:CellStyleSelector x:Key="LRCellStyleSelector">
<converters:CellStyleSelector.LREditedStyle>
<Style TargetType="telerik:GridViewCell">
<Setter Property="Background" Value="Yellow"/>
<Setter Property="FontWeight" Value="Bold"/>
<Setter Property="FontSize" Value="12"/>
</Style>
</converters:CellStyleSelector.LREditedStyle>
</converters:CellStyleSelector>
<converters:CellStyleSelector x:Key="TargetCellStyleSelector">
<converters:CellStyleSelector.TargetEditedStyle>
<Style TargetType="telerik:GridViewCell">
<Setter Property="Background" Value="Yellow"/>
<Setter Property="FontWeight" Value="Bold"/>
<Setter Property="FontSize" Value="12"/>
</Style>
</converters:CellStyleSelector.TargetEditedStyle>
</converters:CellStyleSelector>
<converters:CellStyleSelector x:Key="URCellStyleSelector">
<converters:CellStyleSelector.UREditedStyle>
<Style TargetType="telerik:GridViewCell">
<Setter Property="Background" Value="Yellow"/>
<Setter Property="FontWeight" Value="Bold"/>
<Setter Property="FontSize" Value="12"/>
</Style>
</converters:CellStyleSelector.UREditedStyle>
</converters:CellStyleSelector>
</ResourceDictionary>
</UserControl.Resources>

//GridViewDataColumns

<telerik:GridViewDataColumn Header="New Target" DataMemberBinding="{Binding Path=NewTarget}" IsReadOnly="False" CellStyleSelector="{StaticResource TargetCellStyleSelector}"></telerik:GridViewDataColumn>
<telerik:GridViewDataColumn Header="New High Limit" DataMemberBinding="{Binding Path=NewUR}" IsReadOnly="False" CellStyleSelector="{StaticResource URCellStyleSelector}"></telerik:GridViewDataColumn>

xaml.cs

private void SpecGrid_CellEditEnded(object sender, Telerik.Windows.Controls.GridViewCellEditEndedEventArgs e)
{
OperationStepInfoViewModel item = e.Cell.ParentRow.Item as OperationStepInfoViewModel;
if (item != null)
{
if (e.Cell.Column.UniqueName == "NewLR")
{
if (e.OldData.ToString() != e.NewData.ToString())
item.LRCellChanged = true;
else
item.LRCellChanged = false;
}
if (e.Cell.Column.UniqueName == "NewTarget")
{
if (e.OldData.ToString() != e.NewData.ToString())
item.TargetCellChanged = true;
else
item.TargetCellChanged = false;
}
if (e.Cell.Column.UniqueName == "NewUR")
{
if (e.OldData.ToString() != e.NewData.ToString())
item.URCellChanged = true;
else
item.URCellChanged = false;
}
}

}

CellStyleSelector.cs:

namespace SpecificationAssistant.DataConverters
{
public class CellStyleSelector : StyleSelector
{
public override Style SelectStyle(object item, DependencyObject container)
{
if (item is OperationStepInfoViewModel)
{
OperationStepInfoViewModel opsivw = item as OperationStepInfoViewModel;
Style style = new Style();
if (opsivw.LRCellChanged)
style = LREditedStyle;
if (opsivw.URCellChanged)
style = UREditedStyle;
if (opsivw.TargetCellChanged)
style = TargetEditedStyle;
return style;
}
else
return null;
}
 
public Style LREditedStyle { get; set; }
public Style TargetEditedStyle { get; set; }
public Style UREditedStyle { get; set; }
}
}

Please advise.
0
K
Top achievements
Rank 1
answered on 18 Mar 2014, 05:09 PM
Hi Yoan,

It was a mistake in the logic on my part.  I have changed the logic by creating three separate styles for each cell and it is working great now.  If there is a better solution please advise but otherwise thanks so much for your help.  It is working like a charm!
0
Yoan
Telerik team
answered on 21 Mar 2014, 01:08 PM
Hi,

This approach seems fine to me. Please, don't hesitate to ask if you need any further help.

Regards,
Yoan
Telerik
 

Build cross-platform mobile apps using Visual Studio and .NET. Register for the online webinar on 03/27/2014, 11:00AM US ET.. Seats are limited.

 
Tags
GridView
Asked by
Beckie
Top achievements
Rank 1
Answers by
Yavor Georgiev
Telerik team
K
Top achievements
Rank 1
Yoan
Telerik team
Share this question
or