title: Databinding: using the static resource (assigning programatically?) DataGrid
body: <p>Perhaps it would be better if I explained why I need to do this.</p>
  
  <p>This is not the true example, but in my working solution, the user will start with a relation group like this:</p>
  
  <p>Apple -> Red
  Banana -> Yellow</p>
  
  <p>In the app, in different screens (e.g. Add fruit and add color) they have the power to add new fruits/colors. They then need to link these in this screen I am building here, but also need to be able to change the relationship. So as such, I cannot define the list hardcoded in xaml, as the user can change it. So I need to load this list and then display it in the Comboboxes. So far none of the solutions below have worked to achieve this.</p>
  
  <p>Have a question regarding StaticResource in the context below:</p>
  
  <pre><code>&lt;toolkit:DataGridTextColumn Header="Name"
           Binding="{Binding Name}"
           Width="5*" /&gt;
  
  &lt;toolkit:DataGridComboBoxColumn Header="Color"
           ItemsSource="{Binding Source={StaticResource AllColors}}"
           SelectedValueBinding="{Binding Path=Color}"
           TextBinding="{Binding Path=Color}"
           Width="5*" /&gt;
  </code></pre>
  
  <p>Where AllColors would be defined as:</p>
  
  <pre><code>&lt;x:Array x:Key="AllColors"
           Type="sys:String"&gt;
      &lt;sys:String&gt;Orange&lt;/sys:String&gt;
      &lt;sys:String&gt;Yellow&lt;/sys:String&gt;
  &lt;/x:Array&gt;
  </code></pre>
  
  <p>Except what I really want to do is set that StaticResource programmatically, to a list or array of strings.</p>
  
  <p>So how could I do that?</p>
  
  <p>Thanks.</p>
  
  <p><strong>Edit 1</strong></p>
  
  <p>Here's what I tried:</p>
  
  <pre><code>        // find resource object
  
          //var resource = (string[])Resources["Colors"];
          var i = 0;
          var colors = new string[] { };
  
          foreach (var fruit in fruitList)
                          {
                              colors[i] = fruit.Color;
                              i++;
                          }
  
          Resources["Colors"] = colors;
  </code></pre>
  
  <p>Doesn't work.</p>
  
  <p>Please help.</p>
  
  <p><strong>Edit 2:</strong> my complete code - should make it more obvious how I am strugglign with defining the resource programatically</p>
  
  <pre><code>&lt;UserControl x:Class="Wpf.Screen"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:toolkit="http://schemas.microsoft.com/wpf/2008/toolkit" xmlns:Wpf="clr-namespace:Wpf" MinHeight="300" MinWidth="300" Loaded="Screen_Loaded"
      Name="Fruits"&gt;
  
  &lt;GroupBox&gt;
              &lt;toolkit:DataGrid Name="dgFruits" 
              AutoGenerateColumns="False"
              Margin="10"
              ItemsSource="{Binding}"&gt;
  
                  &lt;toolkit:DataGrid.Columns&gt;
  
                      &lt;toolkit:DataGridTextColumn
                          Binding="{Binding Name}"
                          Header="Fruit Name"
                          Width="5*"/&gt;
  
                      &lt;toolkit:DataGridComboBoxColumn Header="Color"
                                           ItemsSource="{Binding Source={StaticResource AllColors1}}"
                                          SelectedValueBinding="{Binding Path=Color}"
                                          TextBinding="{Binding Path=Color}"
                                          Width="5*" /&gt;
  
                  &lt;/toolkit:DataGrid.Columns&gt;
              &lt;/toolkit:DataGrid&gt;
  
          &lt;/GroupBox&gt;
  </code></pre>
  
  <p>C#:</p>
  
  <pre><code>namespace Wpf
  {
      public partial class Screen
  
  public ObservableCollection&lt;Fruit&gt; FruitCollection { get { return fruitCollection; } }
  
      public Screen()
          {
              LoadFruitFile(); //this loads fruit into ObservableCollection&lt;Fruit&gt; fruitCollection             
              InitializeComponent();
          }
  
  private void Screen_Loaded(object sender, System.Windows.RoutedEventArgs e)
          {
  
              var i = 0;
              var colors = new string[] { };
  
              foreach (var fruit in fruitList)
              {
                  colors[i] = fruit.Color;
                  i++;
              }
  
              // define resource in the code
              Resources["AllColors1"] = colors;
              // show new values
              var resource = (string[])Resources["AllColors1"];
              if (resource != null)
                  foreach (var item in resource)
                      Console.WriteLine(item);
  
  
              dgFruits.ItemsSource = FruitCollection;
  
  
  }
  </code></pre>