使用 WPF 中的字典进行双向数据绑定 [英] Two Way Data Binding With a Dictionary in WPF
问题描述
我想在 WPF 中将 Dictionary
绑定到 ListView
.我希望通过数据绑定机制来更新 Dictionary
中的 Values
.我不想改变 Keys
只是 Values
.我也不关心向 Dictionary
添加新映射.我只想更新现有的.
将字典设置为 ListView
的 ItemsSource
并不能实现这一点.它不起作用,因为 ListView
使用 Enumerator 访问 Dictionary
的内容,并且该枚举的元素是不可变的 KeyValuePair
对象.
我目前的调查尝试使用 Keys
属性.我将此分配给我的 ListView
的 ItemsSource
属性.这确实允许我显示 Keys
,但我对 WPF 的数据绑定机制了解不够,无法访问 Dictionary
中的 Values
.>
我发现了这个问题:在 XAML 中访问代码隐藏变量 但仍然不能似乎弥合了差距.
你们中有人知道如何使这种方法奏效吗?有人有更好的方法吗?
看起来,作为最后的手段,我可以构建一个自定义对象并将其粘贴在 List
中,我从中重新创建/更新我的 Dictionary
但这似乎一种绕过内置数据绑定功能而不是有效利用它的方法.
我不认为你能用字典做你想做的事.
- 因为字典没有实现 INotifyPropertyChanged 或 INotifyCollectionChanged
- 因为它不会像您想要的那样允许双向绑定.
我不确定它是否完全符合您的要求,但我会使用 ObservableCollection
和自定义类.
我正在使用 DataTemplate,以展示如何以两种方式对值进行绑定.
当然在这个实现中没有使用 Key,所以你可以只使用 ObservableCollection
自定义类
公共类 MyCustomClass{公共字符串密钥 { 获取;放;}公共 int 值 { 得到;放;}}
设置项目来源
ObservableCollectiondict = new ObservableCollection();dict.Add(new MyCustomClass{Key = "test", Value = 1});dict.Add(new MyCustomClass{ Key = "test2", Value = 2 });listView.ItemsSource = 字典;
XAML
<DataTemplate x:Key="ValueTemplate"><TextBox Text="{绑定值}"/></数据模板></Window.Resources><ListView Name="listView"><ListView.View><网格视图><GridViewColumn CellTemplate="{StaticResource ValueTemplate}"/></GridView></ListView.View></ListView>
I'd like to bind a Dictionary<string, int>
to a ListView
in WPF. I'd like to do this in such a way that the Values
in the Dictionary
get updated via the data binding mechanism. I don't want to change the Keys
just the Values
. I also don't care about adding new mappings to the Dictionary
. I just want to update existing ones.
Setting the Dictionary as the ItemsSource
of the ListView
doesn't accomplish this. It doesn't work because the ListView
uses the Enumerator to access the contents of the Dictionary
and the elements of that enumeration are immutable KeyValuePair
objects.
My current line of investigation attempts to use the Keys
property. I assign this to the ItemsSource
property of my ListView
. This does allow me to display the Keys
but I don't know enough about WPF's databinding mechanism to access the Values
in the Dictionary
.
I found this question: Access codebehind variable in XAML but still can't seem to bridge the gap.
Do any of you know how to make this approach work? Does anyone have a better approach?
It seems like, as a last resort, I could build a custom object and stick it in a List
from which I recreate/update my Dictionary
but this seems like a way to circumvent the built-in data binding functionality rather than effectively utilize it.
I don't think you're going to be able to do what you'd like with a dictionary.
- Because the Dictionary doesn't implement INotifyPropertyChanged or INotifyCollectionChanged
- Because it's not going to allow two way binding like you want.
I'm not sure if it fits your requirements exactly, but I would use an ObservableCollection
, and a custom class.
I'm using a DataTemplate, to show how to make the binding on the values two way.
Of course in this implementation Key isn't used, so you could just use an ObservableCollection<int>
Custom Class
public class MyCustomClass
{
public string Key { get; set; }
public int Value { get; set; }
}
Set ItemsSource
ObservableCollection<MyCustomClass> dict = new ObservableCollection<MyCustomClass>();
dict.Add(new MyCustomClass{Key = "test", Value = 1});
dict.Add(new MyCustomClass{ Key = "test2", Value = 2 });
listView.ItemsSource = dict;
XAML
<Window.Resources>
<DataTemplate x:Key="ValueTemplate">
<TextBox Text="{Binding Value}" />
</DataTemplate>
</Window.Resources>
<ListView Name="listView">
<ListView.View>
<GridView>
<GridViewColumn CellTemplate="{StaticResource ValueTemplate}"/>
</GridView>
</ListView.View>
</ListView>
这篇关于使用 WPF 中的字典进行双向数据绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!