Home > Mobile >  Need help trying to attach a dictionary key and value to separate ListView GridViewColumns
Need help trying to attach a dictionary key and value to separate ListView GridViewColumns

Time:10-08

XAML:

<ListView Grid.Row="0" Grid.Column="2" Width="155" Height="Auto" Margin="10" ItemsSource="{Binding MedialDictionary}"></ListView>

C# ViewModel:

private Dictionary<DateTime, decimal> _medialDictionary;
public Dictionary<DateTime, decimal> MedialDictionary
{
    get => _medialDictionary;
    set => SetProperty(ref _medialDictionary, value);
}
  • The dictionary key = DateTime

  • The Dictionary value = decimal

  • WPF looks like this:

    ListView with multiple items like "[10/10/2008 00:00:00, 500]".


I want to have the key in one column of the list and a value in the other. I have added GridView columns inside the ListView, but do not how to bind the properties correctly (EffectiveDate, Value). I have read much documentation, however it has not made any sense to me so I am looking for advice/solutions here.

CodePudding user response:

An item in a Dictionary<K,V> is a KeyValuePair<TKey,TValue>, which is a struct with a Key and a Value property that you can bind.

In order to display a tabular layout with columns, you can use a GridView. Each colum has a DisplayMemberPath property that the corresponding property to display is bound to.

<ListView Grid.Row="0" Grid.Column="2" Width="155" Height="Auto" Margin="10"
          ItemsSource="{Binding MedialDictionary}">
   <ListView.View>
      <GridView>
         <GridViewColumn Header="Date"
                         DisplayMemberBinding="{Binding Key}"/>
         <GridViewColumn Header="Value"
                         DisplayMemberBinding="{Binding Value}"/>
      </GridView>
   </ListView.View>
</ListView>
  • Related