开发者

Displaying an array of strings in a certain way

开发者 https://www.devze.com 2023-02-13 12:10 出处:网络
I have a string[] in WPF that is a list of artists. I want to display them as following: Artist A, Artist B, Artist C (this being 3 items in the array).Each of the items needs to be clickable and I ne

I have a string[] in WPF that is a list of artists. I want to display them as following: Artist A, Artist B, Artist C (this being 3 items in the array). Each of the items needs to be clickable and I need to be able to act on the clicks. This view shall be put into a ListView of songs that is using a GridView (this works already, but currently it only displays System.String[] in the artists-cells).

When the user clicks on an artist, I need to know what song index and what artist-index that was clicked (or what song index and what artist (the string), then I can just use IndexOf() to find the artist-index). Also, I don't want scrollbars to be shown in the cell, so if the array is too long the content should be clipped.

How would I make something like this?

[Edit]

What I have so far is this:

<ListView x:Name="SpotifySongs" Grid.Row="3" Grid.Column="1" BorderThickness="0" ItemContainerStyle="{StaticResource ResourceKey=TrackListRowStyle}"
          >
    <ListView.View>
        <GridView AllowsColumnReorder="False">
            <GridViewColumn Width="25" Header="">
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <Image Source="{Binding Path=IsStarred, Converter={StaticResource ResourceKey=StarredConverter}}" Height="13" Width="13" Margin="0" />
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
            <GridViewColumn Width="150" Header="Track" DisplayMemberBinding="{Binding Path=Name}"></GridViewColumn>
            <GridViewColumn Width="150" Header="Artist">
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <ListView ItemsSource="{Binding Path=Artists}" BorderThickness="0" ScrollViewer.CanContentScroll="False" Background="Transparent">
                            <ListView.ItemsPanel>
                                <ItemsPanelTemplate>
                                    <StackPanel Orientation="Horizontal" CanHorizontallyScroll="False" CanVerticallyScroll="False" ClipToBounds="True"
                                                Background="Transparent">
                                    开发者_JAVA技巧</StackPanel>
                                </ItemsPanelTemplate>
                            </ListView.ItemsPanel>
                            <ListView.ItemTemplate>
                                <DataTemplate>
                                    <TextBlock Foreground="{Binding Path=Foreground, RelativeSource={RelativeSource AncestorType=ListViewItem, AncestorLevel=2}}"
                                               Background="Transparent"
                                               Text="{Binding}"/>
                                </DataTemplate>
                            </ListView.ItemTemplate>
                        </ListView>
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
            <GridViewColumn Width="50" Header="Time" DisplayMemberBinding="{Binding Path=Length}"></GridViewColumn>
            <GridViewColumn Width="150" Header="Album" DisplayMemberBinding="{Binding Path=Album}"></GridViewColumn>
        </GridView>
    </ListView.View>
</ListView>

The Artist-column beeing the artists. There are 3 problems, the first one is to get a , between the artist-names, the second is to remove the scrollbars and enable clipping if there are two many artists to display within 150px, and the last one (I believe I've solved that one) is the clicking.

Notifications on update isn't important as I keep swapping the source when there is new data (because it's normally all new).


I would use a List object as ListView's DataSource is an ICollection or IList which String[] is not.

    List<String> stringList = new List<string>();

    stringList.Add("Artist 1");
    stringList.Add("Artist 2");
    stringList.Add("Artist 3");
    stringList.Add("Artist 4");

    lsv.ItemsSource = stringList;

XAML

<ListView x:Name="lsv" />
0

精彩评论

暂无评论...
验证码 换一张
取 消