2

I have used WPF very little so I'm looking for the simplest most straightforward way to accomplish this task.

Basically I have a Dictionary where the key is some identifier, and the value is a description. I wish to display a grid/listview of the descriptions with the intention of adding/removing rows by their non-displayed identifier.

How can this be accomplished quickly and easily?

kmarks2
  • 4,755
  • 10
  • 48
  • 77
  • Connect the listview to your data with the ContextData property of the listview. – Michel Keijzers Aug 22 '12 at 15:03
  • if it is a dictionary, how do you plan to set Keys if its column is not displayed? – Artiom Aug 22 '12 at 15:03
  • @Artiom The keys and values will have been set somewhere else in the application already. – kmarks2 Aug 22 '12 at 15:07
  • @kmarks2 but you said you wish to add rows, but you won't be able to add value to dictionary because the key for new row won't be set. Just in case, here is how you can just update dictionary http://stackoverflow.com/questions/800130/two-way-data-binding-with-a-dictionary-in-wpf – Artiom Aug 22 '12 at 15:12
  • @kmarks2 What for do you need to bind the dictionary? May be you can bind not only to the dictionary. – Artiom Aug 23 '12 at 01:47

2 Answers2

1

Sample code binding to a dictionary list. To display the value replace Key with Value. Dictionary is not an observable collection so the UI will not dynamically pick up inserts and deletes.

<ListView ItemsSource="{Binding Path=GabeLib.DLFTSwordReverse, Mode=OneWay}" DisplayMemberPath="Key" 
                                      VirtualizingStackPanel.IsVirtualizing="True" VirtualizingStackPanel.VirtualizationMode="Recycling" ScrollViewer.IsDeferredScrollingEnabled="True"  
                                      ScrollViewer.HorizontalScrollBarVisibility="Disabled" ScrollViewer.VerticalScrollBarVisibility="Visible"/>

If you set Mode=TwoWay I think you could even update the Value.

paparazzo
  • 44,497
  • 23
  • 105
  • 176
  • as far as I understand, it will display the key, but author doesn't want it. And how it will allow to add/remove rows? – Artiom Aug 22 '12 at 15:15
  • Read my answer. "To display the value replace Key with Value." Correct dictionary will not pick up inserts and delete as I stated in the answer. – paparazzo Aug 22 '12 at 15:18
  • sorry, my fault. Anyway he wanted to insert through UI, but not to allow datagrid to be notified about inserts in the dictionary – Artiom Aug 22 '12 at 15:22
  • @Artiom I agree. OP wants an easy Dictionary solution and there is not an easy Dictionary only solution if dynamic inserts and deletes are required. – paparazzo Aug 22 '12 at 15:27
1

You can't do that. To add the new row you need to set it. But in case of hidden Key column you won't accomplish this. In case you need just updating look here

Community
  • 1
  • 1
Artiom
  • 7,694
  • 3
  • 38
  • 45
  • But there are not accepted answers in the link. And I don't think any of those answers are correct. This is an answer on ObservableDictionary http://stackoverflow.com/questions/1098663/general-observable-dictionary-class-for-databinding-wpf-c-sharp but I doubt the OP will make that much effort based on effort shown so far. – paparazzo Aug 22 '12 at 21:59
  • @Blam suppose this question will be also without answers. It can't be accomplished without compromises. – Artiom Aug 23 '12 at 01:46
  • Not arguing with you. It can be done with compromises. But it is cannot be done quickly and easily. +1 just so you don't take me wrong. – paparazzo Aug 23 '12 at 01:53