Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
274 views
in Technique[技术] by (71.8m points)

c# - Two Way Data Binding With a Dictionary in 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.

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I don't think you're going to be able to do what you'd like with a dictionary.

  1. Because the Dictionary doesn't implement INotifyPropertyChanged or INotifyCollectionChanged
  2. 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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...