Perhaps it would be better if I explained why I need to do this.
This is not the true example, but in my working solution, the user will start with a relation group like this:
Apple -> Red Banana -> Yellow
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.
Have a question regarding StaticResource in the context below:
<toolkit:DataGridTextColumn Header="Name"
Binding="{Binding Name}"
Width="5*" />
<toolkit:DataGridComboBoxColumn Header="Color"
ItemsSource="{Binding Source={StaticResource AllColors}}"
SelectedValueBinding="{Binding Path=Color}"
TextBinding="{Binding Path=Color}"
Width="5*" />
Where AllColors would be defined as:
<x:Array x:Key="AllColors"
Type="sys:String">
<sys:String>Orange</sys:String>
<sys:String>Yellow</sys:String>
</x:Array>
Except what I really want to do is set that StaticResource programmatically, to a list or array of strings.
So how could I do that?
Thanks.
Edit 1
Here's what I tried:
// 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;
Doesn't work.
Please help.
Edit 2: my complete code - should make it more obvious how I am strugglign with defining the resource programatically
<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">
<GroupBox>
<toolkit:DataGrid Name="dgFruits"
AutoGenerateColumns="False"
Margin="10"
ItemsSource="{Binding}">
<toolkit:DataGrid.Columns>
<toolkit:DataGridTextColumn
Binding="{Binding Name}"
Header="Fruit Name"
Width="5*"/>
<toolkit:DataGridComboBoxColumn Header="Color"
ItemsSource="{Binding Source={StaticResource AllColors1}}"
SelectedValueBinding="{Binding Path=Color}"
TextBinding="{Binding Path=Color}"
Width="5*" />
</toolkit:DataGrid.Columns>
</toolkit:DataGrid>
</GroupBox>
C#:
namespace Wpf
{
public partial class Screen
public ObservableCollection<Fruit> FruitCollection { get { return fruitCollection; } }
public Screen()
{
LoadFruitFile(); //this loads fruit into ObservableCollection<Fruit> 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;
}