如果我解释为什么需要这样做,也许会更好。
这不是真实的例子,但在我的工作解决方案中,用户将从这样的关系组开始:
Apple - >红色 香蕉 - >黄
在应用程序中,在不同的屏幕(例如添加水果和添加颜色)中,他们有能力添加新的水果/颜色。然后他们需要在我在这里建立的这个屏幕中链接这些,但也需要能够改变关系。因此,我无法在xaml中定义硬编码列表,因为用户可以更改它。所以我需要加载这个列表然后在Comboboxes中显示它。到目前为止,以下解决方案都没有达到目的。
在下面的上下文中有关于StaticResource的问题:
<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*" />
将AllColors定义为:
<x:Array x:Key="AllColors"
Type="sys:String">
<sys:String>Orange</sys:String>
<sys:String>Yellow</sys:String>
</x:Array>
除了我真正想要做的是将StaticResource以编程方式设置为字符串列表或数组。
那我怎么能这样做?
感谢。
修改1
这是我试过的:
// 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;
不起作用。
请帮忙。
编辑2:我的完整代码 - 应该让我更清楚如何以编程方式定义资源?
<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;
}
答案 0 :(得分:3)
您可以使用FrameworkElement的FindResource方法或Resources属性从代码中获取资源对象。检查以下代码是否适合您:
// find resource object
//string[] resource = (string[])FindResource("AllColors");
string[] resource = (string[])Resources["AllColors"];
if (resource != null)
{
// old values
foreach (var item in resource)
Console.WriteLine(item);
// set new values
Resources["AllColors"] = new string[] { "red", "blue", "green"};
}
// find resource and show its new values
resource = (string[])Resources["AllColors"];
if (resource != null)
foreach (var item in resource)
Console.WriteLine(item);
虽然我不认为它会以你想要的方式工作,或者它是你的任务的正确解决方案。你可以做的是用颜色集合定义一个类:
public class TestColors
{
public TestColors()
{
Colors = new ObservableCollection<string>();
Colors.Add("red");
Colors.Add("blue");
Colors.Add("green");
}
public ObservableCollection<string> Colors { get; set; }
}
在xaml中,您可以定义给定对象类型的ObjectDataProvider,并将控件绑定到其Colors属性。像这样的Smth:
...
xmlns:local="clr-namespace:YourNamespace"
...
<Window.Resources>
<ObjectDataProvider x:Key="AllColors0" ObjectType="{x:Type local:TestColors}"/>
</Window.Resources>
...
<ListBox ...
DataContext="{StaticResource AllColors0}"
ItemsSource="{Binding Colors}"/>
...
另一种方法是将控件绑定到窗口的属性,如下所示:
public partial class MainWindow : Window
{
public static string[] TestProperty
{
get
{
List<string> result = new List<string>();
result.Add("red");
result.Add("green");
result.Add("blue");
return result.ToArray();
}
}
public MainWindow()
{
InitializeComponent();
...
XAML:
<ListBox ...
ItemsSource="{x:Static local:MainWindow.TestProperty}"/>
update0 :在代码中定义资源
// define resource in the code
Resources["AllColors1"] = new string[] { "red1", "blue1", "green1" };
// show new values
resource = (string[])Resources["AllColors1"];
if (resource != null)
foreach (var item in resource)
Console.WriteLine(item);
希望这有帮助,尊重