title: Setting background of a list of rows and columns in WPF
body: <h2>The problem</h2>
  
  <p>I'm filling up a datagrid using an xmlprovider for the data. After the loading I'm trying to color "locked" columns and rows based on a list of "locked" rows &amp; columns. "Locking" here is just a visual feedback for a certain row or column not being used for further calculations. While the loading of data works fine, I've tried different solutions to set the background property of CERTAIN rows AND columns by looping through all the cells using extensions from <a href="https://code.google.com/p/artur02/source/browse/trunk/DataGridExtensions/DataGridHelper.cs" rel="nofollow" title="datagrid extions">data grid extensions</a> with mixed success.</p>
  
  <p>I try to color the cells in the LOADED event, but that doesn't always work apparently. And there's no event that says: "your data has been loaded sir, want to do something now"?</p>
  
  <p>So my last resort for coloring rows and columns is probably using style setters with a IMultiValueConverter class. But it's not clear for me how I can send the correct data to the converter.</p>
  
  <h2>The code</h2>
  
  <p>This is what I have for now:</p>
  
  <pre><code>public partial class MainWindow : Window
  {
      public ObservableCollection&lt;Person&gt; MyDynData { get; set; }
  
      // property representing the locked rows
      public List&lt;int&gt; LockedRows { get; set; }
      // property representing the locked colums
      public List&lt;int&gt; LockedColumns { get; set; }
  
      public MainWindow()
      {
          InitializeComponent();
  
          MyDynData = Person.GetData();
          LockedRows = new List&lt;int&gt;{1,3};
          LockedColumns = new List&lt;int&gt;{0};
  
          DataContext = this;
      }
  }
  
  public class Person
  {
      public string FirstName { get; set; }
      public string LastName { get; set; }
      public int Age { get; set; }
  
      public static ObservableCollection&lt;Person&gt;GetData()
      {
          return new ObservableCollection&lt;Person&gt;
          {
              new Person {Age = 19, FirstName = "John", LastName = "Gates"},
              new Person {Age = 25, FirstName = "Jill", LastName = "Vegas"},
              new Person {Age = 48, FirstName = "Bill", LastName = "Bates"},
              new Person {Age = 29, FirstName = "Agnes", LastName = "Henderson"},
              new Person {Age = 33, FirstName = "Jenny", LastName = "Giggles"}
  
          };
      }
  }
  </code></pre>
  
  <p>The XAML:</p>
  
  <pre><code>&lt;Window x:Class="DatagridColRowLocker.MainWindow"
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          xmlns:converters="clr-namespace:DatagridColRowLocker"
          Title="MainWindow" Height="350" Width="525"&gt;
      &lt;Grid&gt;
          &lt;Grid.Resources&gt;
              &lt;converters:CellBackgroundConverter x:Key="CellBackgroundConverter"/&gt;
          &lt;/Grid.Resources&gt;
  
          &lt;DataGrid ItemsSource="{Binding Path=MyDynData}" AutoGenerateColumns="True"&gt;
              &lt;DataGrid.Resources&gt;
                  &lt;Style x:Key="LockedCellStyle" TargetType="DataGridCell"&gt;
                      &lt;Style.Setters&gt;
                          &lt;Setter Property="Background"&gt;
                              &lt;Setter.Value&gt;
                                  &lt;MultiBinding Converter="{StaticResource CellBackgroundConverter}"&gt;
                                      &lt;Binding&gt; ??? the current cell&lt;/Binding&gt;
                                      &lt;Binding&gt; ??? List of locked columns&lt;/Binding&gt;
                                      &lt;Binding&gt; ??? list of locked rows&lt;/Binding&gt;
                                  &lt;/MultiBinding&gt;
                              &lt;/Setter.Value&gt;
                          &lt;/Setter&gt;
                      &lt;/Style.Setters&gt;
                  &lt;/Style&gt;
              &lt;/DataGrid.Resources&gt;
          &lt;/DataGrid&gt;
      &lt;/Grid&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p>The cell background color converter:</p>
  
  <pre><code>class CellBackgroundConverter : IMultiValueConverter
  {
      public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
      {
          var cell = (DataGridCell)values[0];
          var lockedColumns = (List&lt;int&gt;)values[1];
          var lockedRows = (List&lt;int&gt;)values[2];
  
          // how to get the row index? 
          var isLocked = lockedColumns.Contains(cell.Column.DisplayIndex);
  
          return (isLocked) ? new SolidColorBrush(Colors.LightGray) : new SolidColorBrush(Colors.White);
      }
  
      public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
      {
          throw new NotImplementedException();
      }
  }
  </code></pre>
  
  <h2>Summary</h2>
  
  <ul>
  <li>How do I correctly bind my values in xaml for the multiconverter to work</li>
  <li>If I pass a datagridcell to the converter, I can get the displaycolIndex but not the rowIndex, how does that work?</li>
  </ul>