wpf 绑定到静态资源
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16671233/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
wpf binding to static resource
提问by boo_boo_bear
So I have aproblem with binding in WPF. I am trying to bind a DataGrid ComboBoxColumn with a static resource but with no luck. I know where the problem is but I'm not sure how to fix it.
所以我在 WPF 中绑定有问题。我正在尝试将 DataGrid ComboBoxColumn 与静态资源绑定,但没有运气。我知道问题出在哪里,但我不知道如何解决。
in XAML i have this:
在 XAML 我有这个:
<local:MyClasificators x:Key="clList"></local:MyClasificators>
and DataGridComboBoxColumn
和 DataGridComboBoxColumn
<DataTemplate>
<ComboBox ItemsSource="{StaticResource clList}" DisplayMemberPath="Value" ></ComboBox>
</DataTemplate>
code for the source I'm binding:
我绑定的源代码:
public class MyClasificators:List<KeyValuePair<object, object>>
{
public void _MyClasificators(DataTable country)
{
foreach (DataRow row in country.Rows)
{
this.Add(new KeyValuePair<object, object>(row.ItemArray[0], row.ItemArray[1]));
}
}
And the code for passing the DataTable:
以及传递数据表的代码:
public void callMyClassificators(DataTable country)
{
MyClasificators clasif = new MyClasificators();
clasif._MyClasificators(country);
}
I know that most probably I just have to edit the Resource part, but I'm not sure how should I go about it?
我知道很可能我只需要编辑 Resource 部分,但我不确定我应该怎么做?
采纳答案by Marc
<local:MyClasificators x:Key="clList"></local:MyClasificators>
translates to something like:
翻译成类似的东西:
Resources.Add("clList", new MyClasificators());
That's it, there's no data in your object.
就是这样,您的对象中没有数据。
You could create the resource clListfrom code, for example in app.xaml.cs:
您可以clList从代码创建资源,例如在 app.xaml.cs 中:
var countryTable = ... // Get or create table here
var clList = new MyClasificators();
var clList.callMyClassificators(countryTable);
Resources.Add("clList", clList);
回答by RonakThakkar
From above code, it seems that an instance of MyClasificators is created in resource section. But MyClasificators (clList) does not have any items. It's an empty list. Place a break point in your code and check this.Resources["clList"] and check the count of items in it.
从上面的代码来看,似乎在资源部分创建了一个 MyClasificators 的实例。但是 MyClasificators (clList) 没有任何项目。这是一个空列表。在您的代码中放置一个断点并检查 this.Resources["clList"] 并检查其中的项目数。
回答by Kai Wang
I see multiple problems.
我看到多个问题。
in callMyClassificators, you create a new instance of MyClasificators. That instance is not the one you bind in Xaml. When you define a local resource, one instance is created there. That's the one your Combox is bound to, not the one you create in callMyClassificators. You should make sure xaml and code work on the same instance.
Let's say you fix No.1. When is "callMyClassificators" called? After the binding is done, there is no way for your MyClasificators to notify WPF that the list has changed. You could use a ObservableCollection> so that collection change will automatically be observed by WPF.
在 callMyClassificators 中,您创建了一个新的 MyClasificators 实例。该实例不是您在 Xaml 中绑定的实例。当您定义本地资源时,会在那里创建一个实例。那是您的 Combox 所绑定的那个,而不是您在 callMyClassificators 中创建的那个。您应该确保 xaml 和代码在同一个实例上工作。
假设您修复了 1 号。什么时候调用“callMyClassificators”?绑定完成后,您的 MyClasificators 无法通知 WPF 列表已更改。您可以使用 ObservableCollection> 以便 WPF 自动观察集合更改。

