简体   繁体   中英

double lines in listview c# uwp

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

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);

        }

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM