数据绑定:使用静态资源(以编程方式分配?)DataGrid
如果我解释为什么我需要这样做,也许会更好。数据绑定:使用静态资源(以编程方式分配?)DataGrid
这不是真实的例子,但在我工作的解决方案,用户将与关系组这样开始:
苹果 - >红 香蕉 - >黄色
在应用中,在不同的屏幕(例如添加水果和添加颜色),他们有能力添加新的水果/颜色。然后他们需要将这些屏幕链接到我在这里创建的屏幕中,但也需要能够改变这种关系。因此,我无法定义在xaml中硬编码的列表,因为用户可以更改它。所以我需要加载这个列表,然后将它显示在Combobox中。到目前为止,下面的解决方案都没有实现这个目标。
有以下方面有关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>
除了我真正想要做的是设置静态资源编程,到列表或字符串数组。
那么我该怎么做?
谢谢。
编辑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:我的完整代码 - 应该使它更加明显如何,我strugglign与定义资源编程
<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;
}
,你可以得到的资源从反对你代码使用FindResource方法或Resources FrameworkElement的属性。检查下面的代码会为你工作:
// 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的和绑定你的控制,它的颜色属性。水木清华这样的:
...
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);
定义资源
希望这会有所帮助,关于
到目前为止尝试了您的第一个和最后一个建议。 首先没有工作也找不到AllColors。 无法找到名为 '{} AllColors' 资源。资源名称区分大小写。标记文件'Wpf; component/screen.xaml'中对象'System.Windows.Data.Binding'的错误行48位41. 第二次没有工作我得到这个错误: 异常已被目标抛出的调用。 “;组件/ screen.xaml WPF”线48的位置41,其中 – baron 2010-01-29 01:24:38
AllColors被声明在对象中的标记文件“Microsoft.Windows.Controls.DataGridComboBoxColumn”错误?尝试将其移至xaml的Window.Resources部分。我实际上测试过提供的代码,它对我很有用。但正如我之前所说的那样;这不是这样做的方式;看是否建议#2或#3会为你 – 2010-01-29 02:51:08
,我想我是在代码中有定义它... – baron 2010-01-29 03:38:13
似乎是一个相当普遍的数据绑定需求,不明白为什么它很难实现! – baron 2010-02-03 00:43:37
你能否扩展为什么重要的是做一个静态绑定?你为什么不绑定到你的代码背后的文件属性? – 2010-02-07 21:50:53
如果我能弄清楚如何去做,那会很好。我可以绑定到List或在代码隐藏中创建ObservableCollection并绑定到其中任何一个,但我无法弄清楚如何在代码隐藏中执行此操作。 – baron 2010-02-08 01:24:30