Ayda Sayed
Ayda Sayed

Reputation: 509

How to change RowBackground color of a datagrid

I am currently working on a WPF application MVVM pattern where I have to create some sort of relationship between a treeview and a grid. The relationship is based on ID. The idea is to highlight a row with a id equal to the treenode id.

Display color property

    public Brush DisplayColor
    {
        set
        {
            _displayColor = value;
            NotifyPropertyChanged("DisplayColor");
        }

        get { return _displayColor; }

    }

Select TreeNode value.id

    private MessageElementViewModel _selectedMessageElement;
    public MessageElementViewModel SelectedMessageElement
    {
        set
        {
            if (_selectedMessageElement == value) return;
            this._selectedMessageElement = value;
            SearchGrid(value.Id, messageFields);

        }
        get
        {
            return this._selectedMessageElement;
        }

    }

// search matching Id in the grid

     public void SearchGrid(int id, ObservableCollection<MessageFieldViewModel> msgField)
    {
         if (msgField.Any())
            DisplayColor = msgField.Last().Id == id ? Brushes.CadetBlue : Brushes.Black;
    }

XAML: Call the display color property to highlight a matching id. DataGrid

TreeView:

      <TreeView Margin="644,137,6,6" Grid.RowSpan="2" ItemsSource="{Binding MessageElements[0].Children,  Mode=TwoWay}"  

                  SelectedItemChanged="TreeView_OnSelectedItemChanged">

Code Behind for treeview select item;

    readonly MainWindowModel _mainWindowModel = new MainWindowModel();

    private void TreeView_OnSelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
    {
        if (_mainWindowModel != null)
            _mainWindowModel.SelectedMessageElement = (MessageElementViewModel)e.NewValue;
    }

EDIT:

<DataGrid   ItemsSource="{Binding MessageFields}" Margin="4,0,380,6" Grid.Row="2" AutoGenerateColumns="False"  IsReadOnly="True"   SelectedValue="{Binding SelectedMessageField}"
                    RowBackground="{Binding Path=DisplayColor}">
            <DataGrid.Columns >
                <DataGridTextColumn Header="ID" Binding="{Binding Id}" Width="*"  />      <!--Foreground="{Binding Path=DisplayColor}-->

                <DataGridTextColumn Header="Code" Binding="{Binding Code}" Width="*" />
                <DataGridTextColumn Header="Field Name" Binding="{Binding Name}" Width="*" />
                <DataGridTextColumn Header="Position" Binding="{Binding Position}" Width="*"   />
                <DataGridTextColumn Header="Length" Binding="{Binding Length}" Width="*"  />
 </DataGrid.Columns>

Why is my is my display color property is not working for matching Ids?

Thank you all.

Upvotes: 0

Views: 1383

Answers (1)

WiiMaxx
WiiMaxx

Reputation: 5420

If you are using the MVVM pattern please don't define Stuff from the VisualTree in your ViewModel like Windows, DataGrids, Brushes, and so on

so now to your question

this is an Simple example for:
How to change RowBackground color of a datagrid

XAML

    <DataGrid ItemsSource="{Binding Source}">
        <DataGrid.Resources>
            <Style TargetType="{x:Type DataGridRow}">
                <Setter Property="Background" Value="Red"/>
                <Style.Triggers>
                    <DataTrigger Binding="{Binding ColorSwitch}" Value="false">
                        <Setter Property="Background" Value="Green"/>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </DataGrid.Resources>
    </DataGrid>

Codebehind

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        DataContext = new VM();
    }
}

ViewModel

public class VM
{
    public List<myItem> Source {get;set;}

    public VM()
    {
        Source = new List<myItem>();
        Source.Add(new myItem{Field1 = "some Text", Field2 = "some other Text",ColorSwitch=false});
        Source.Add(new myItem{Field1 = "some Text", Field2 = "some other Text",ColorSwitch=false});
        Source.Add(new myItem{Field1 = "some Text", Field2 = "some other Text",ColorSwitch=true});
        Source.Add(new myItem{Field1 = "some Text", Field2 = "some other Text",ColorSwitch=false});
        Source.Add(new myItem{Field1 = "some Text", Field2 = "some other Text",ColorSwitch=true});
    }
}

Simpleobject

public class myItem
{
    public string Field1 {get;set;}
    public string Field2 {get;set;}
    public bool ColorSwitch {get;set;}
}

Upvotes: 2

Related Questions