以某种方式显示字符串数组

发布于 2024-10-19 13:06:16 字数 3343 浏览 1 评论 0原文

我在 WPF 中有一个 string[] ,它是艺术家列表。我想将它们显示如下:Artist A、Artist B、Artist C(这是数组中的 3 项)。每个项目都需要可点击,并且我需要能够对点击进行操作。该视图应放入使用 GridView 的歌曲 ListView 中(这已经可以工作,但目前它仅显示 System.String[] 在艺术家细胞中)。

当用户点击某个艺术家时,我需要知道点击的歌曲索引和艺术家索引(或者什么歌曲索引和哪个艺术家(字符串),然后我可以使用 IndexOf() 查找艺术家索引)。另外,我不希望在单元格中显示滚动条,因此如果数组太长,则应剪切内容。

我该如何制作这样的东西?

[编辑]

到目前为止我所拥有的是:

<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">
                                    </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>

Artist - 列是艺术家。有 3 个问题,第一个问题是在艺术家姓名之间获取 ,,第二个问题是如果 150px 内有两个艺术家要显示,则删除滚动条并启用剪辑,最后一个是其中之一(我相信我已经解决了这个问题)是点击。

更新通知并不重要,因为当有新数据时我会不断交换源(因为通常都是新的)。

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">
                                    </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).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

勿忘初心 2024-10-26 13:06:16

我会使用 List 对象
因为 ListView 的数据源是 ICollection 或 IList
String[] 不是。

    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" />

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" />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文