双向数据绑定随着WPF字典 [英] Two Way Data Binding With a Dictionary in WPF

查看:359
本文介绍了双向数据绑定随着WPF字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想绑定词典<字符串,整数> 的ListView 在WPF。我希望做这样的方式,在词典通过数据绑定机制得到更新。我不想更改。我也不在乎添加新映射词典。我只是想更新现有的。

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.

设置字典作为的ItemsSource 的ListView 没有做到这一点。这是行不通的,因为的ListView 使用枚举器访问词典的内容和枚举的元素是不可改变的 KeyValuePair <​​/ code>的对象。

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.

我目前调查的行会尝试使用属性。我这个分配给我的的ListView 的ItemsSource 属性。这并不让我显示,但我不知道有足够的了解WPF的数据绑定机制来访问中在词典

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.

我发现了这样一个问题:访问$ C在XAML $ cbehind变量但似乎仍不能弥补缺口。

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.


  1. 因为字典里没有实现INotifyPropertyChanged和INotifyCollectionChanged

  2. 因为它不会允许双向绑定就像你想要的。

我不知道它是否适合你的要求完全是,但我会使用的ObservableCollection ,和自定义类。

I'm not sure if it fits your requirements exactly, but I would use an ObservableCollection, and a custom class.

我使用一个DataTemplate,以显示如何在价值观结合双向。

I'm using a DataTemplate, to show how to make the binding on the values two way.

当然,在这个实现的关键是不使用,所以你可以只使用一个的ObservableCollection&LT; INT&GT;

Of course in this implementation Key isn't used, so you could just use an ObservableCollection<int>

自定义类

public class MyCustomClass
{
    public string Key { get; set; }
    public int Value { get; set; }
}

设置的ItemsSource

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆