Ticket T326950
Visible to All Users

WPF GridControl dynamic row color

created 9 years ago

Hi,

I am trying to apply functionality to dynamically change grid's row color.
Went through a number of examples, and… I can't get it to work.

Program runs into converter, returns a brush, but row's color remains unchanged. I use Office2010Silver theme.

Could you point what I made wrong?

Here is converter code:

C#
class ItemTypeToColorConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { if (targetType.Name != typeof(Brush).Name) { throw new InvalidOperationException("ItemTypeToColorConverter: TargetType must be a Brush."); } Brush b = Brushes.Blue; if (((DataRowView)value)== null) return Brushes.White; switch(((DataRowView)value).Row.Field<string>("rodzaj_poz")) { case "L": b = Brushes.Red; break; case "D": b = Brushes.RoyalBlue; break; default: b = Brushes.White; break; } return b; } public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } }

And XAML:

XAML
<dxg:GridControl.View> <dxg:TableView MultiSelectMode="Row" NavigationStyle="Row" ShowGroupPanel="True" > <dxg:TableView.RowStyle> <Style TargetType="{x:Type dxg:GridRowContent}" BasedOn="{StaticResource {dxgt:GridRowThemeKey ResourceKey=RowStyle, ThemeName=Office2010Silver, IsThemeIndependent=True}}"> <Setter Property="Background" Value="{Binding Path=Row, Converter={StaticResource ResourceKey=ItemTypeToColor}}" /> </Style> </dxg:TableView.RowStyle> </dxg:TableView> </dxg:GridControl.View>

I would appreciate any hint.

Best regards,
Radek

XAML

Answers approved by DevExpress Support

created 9 years ago

Hi Radek,
I have prepared a sample based on your scenario, and as far as I can see, rows are initially colored. However, the row color is not updated when I modify the underlying cell value. Is this the issue you faced? if so, it is the expected behavior since DataRowView is not changed when you change its field, and the binding is not triggered. To resolve the problem, you can bind directly to the required field.

XAML
<Setter Property="Background" Value="{Binding Path=Row.[rodzaj_poz], Converter={StaticResource ResourceKey=modifiedItemTypeToColor}}" />

I have attached a sample for your reference. Let me know if it helps.

    Comments (2)
    RC RC
    Radosław Czwojdrak 9 years ago

      Hi Illya,
      I am sorry for late answer (Christmas time). I hope you spent some good time with you family :)
      Thank you for replying. After I struggled for a while with this issue, I found what the problem was. I used Brushes class from wrong namespace - System.Windows.Drawing instead of System.Windows.Media, and now it works as expected :)
      Best regards,
      Radek

      DevExpress Support Team 9 years ago

        Hi Radek,
        I'm happy to hear that the issue has been resolved. Should you have any questions regarding our controls, feel free to contact us.

        Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

        Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.