Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...