Reputation: 391
I'm trying to solve this problem but I was not able to find a solution.. I have a dictionary(key, value) as this:
private Dictionary<string, string> myDictionary = new Dictionary<string, string>();
this dictionay is filled with some values (string, string), in this way:
private void LoadData()
{
myDictionary = FillMyDictionary();
foreach (var str in myDictionary)
{
myDictionayList.Items.Add(str.Key);
}
}
In the Xaml file I show the dictionary in a listview like this:
<ListView x:Name="myDictionayList" Margin="0,25,0,0" HorizontalAlignment="Center" VerticalAlignment="Center" Width="250" Height="640" FontSize="12" IsDoubleTapEnabled="False" IsHoldingEnabled="False" IsRightTapEnabled="False" IsTapEnabled="False" IsEnabled="False" IsHitTestVisible="False" Background="Transparent">
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="HorizontalContentAlignment" Value="Center"/>
<Setter Property="MinHeight" Value="25"/>
</Style>
</ListView.ItemContainerStyle>
</ListView>
In this way I'm able to show the key values of my dictionary in the ListView. Now, I would like to add a second row where I put also the Value of the dictionary, obtaining something like this:
thanks for your suggestions
Upvotes: 1
Views: 232
Reputation: 2581
You can do that easily by editing the ItemTemplate
of the list view and adding StackPanel
in that like this:
<ListView x:Name="MyListView">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel >
<TextBlock Text="{Binding Key}"/>
<TextBlock Text="{Binding Value}"/>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
and the ItemSource of the ListView
obviously goes to a dictionary in the code-behind:
MyListView.ItemsSource = new Dictionary<string, string>
{
{"Item1", "Description1"},
{"Item2", "Description2" }
};
which produces result like this:
I think this is what you want. Hope that helps.
Upvotes: 1
Reputation: 1312
I think you meant myDictionayList.Items.Add(str.Key) in your question's code.
Are you looking for something like this?
<ListView x:Name="myDictionayList" Margin="0,25,0,0" HorizontalAlignment="Center" VerticalAlignment="Center" Width="250" Height="640" FontSize="12" IsDoubleTapEnabled="False" IsHoldingEnabled="False" IsRightTapEnabled="False" IsTapEnabled="True" IsEnabled="True" IsHitTestVisible="False" Background="Transparent">
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="HorizontalContentAlignment" Value="Center"/>
<Setter Property="MinHeight" Value="25"/>
</Style>
</ListView.ItemContainerStyle>
<ListView.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<TextBlock Text="{Binding Key}" Grid.Row="0"></TextBlock>
<TextBlock Text="{Binding Value}" Grid.Row="1" Foreground="Gray"></TextBlock>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
Then you can simply add the dictionary item to the myDictionayList listview and style each row however, I just set it as gray.
foreach (var str in myDictionary)
{
myDictionayList.Items.Add(str);
}
Upvotes: 1