C# WPF:带有图标视图的 ListView?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/484293/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 05:13:19  来源:igfitidea点击:

WPF: ListView with icons view?

c#wpflistview

提问by user57528

I can't figure out how I can implement an Icon View in the WPF ListView (a view similar to the Windows Explorer). Searching on google I only found informations about implementing the GridView but no clues about the Icon View. I'm not talking about System.Windows.Form.ListViewbut System.Windows.Controls.ListView.

我不知道如何在 WPF ListView(类似于 Windows 资源管理器的视图)中实现图标视图。在谷歌上搜索我只找到了关于实现 GridView 的信息,但没有找到关于 Icon View 的线索。我不是在谈论System.Windows.Form.ListView但是System.Windows.Controls.ListView

Perhaps there is another control to do that? I didn't find anything relevant about this?

也许还有另一个控件可以做到这一点?我没有找到与此相关的任何内容?

I've only found some people that build the icon view by hand using the listbox and changing the paneltemplate and the icontemplate. I can't believe this is the only way to do it.

我只发现一些人使用列表框手动构建图标视图并更改面板模板和图标模板。我不敢相信这是唯一的方法。

Any clues?

有什么线索吗?

Thanks in advance

提前致谢

采纳答案by geofftnz

Same as Tanveer Badar's answer, but with a WrapPanel instead of a UniformGrid. Set the following in your listbox:

与 Tanveer Badar 的回答相同,但使用 WrapPanel 而不是 UniformGrid。在您的列表框中设置以下内容:

ScrollViewer.HorizontalScrollBarVisibility="Disabled" 
ScrollViewer.VerticalScrollBarVisibility="Auto"       

to force the WrapPanel to wrap.

强制 WrapPanel 换行。

回答by Tanveer Badar

Just off the top of my head, have you tried this?

就在我的头顶上,你试过这个吗?

<Style TargetType="ListBox">
  <Setter Property="ItemsPanel">
    <Setter.Value>
      <ItemsPanelTemplate>
        <UniformGrid/>
      </ItemsPanelTemplate>
    </Setter.Value>
  </Setter>
</Style>

回答by Bubblewrap

EDITAppears i misunderstood what you meant with Explorer view...i have mine set to Details... ;) I'll leave my answer up here in case anyone makes the same mistake as i...

编辑似乎我误解了你对资源管理器视图的意思......我将我的设置为详细信息......



There is no such thing as an Icon View in WPF, you'll have to implement it yourself, but you dont have to do everything from scratch.

WPF 中没有图标视图这样的东西,您必须自己实现它,但您不必从头开始做所有事情。

You can use the ListView in combination with a GridView and at least one CellTemplate for the column that contains the icon.

对于包含图标的列,您可以将 ListView 与 GridView 和至少一个 CellTemplate 结合使用。

The general outline would look something like this for an Windows Explorer like view:

对于类似 Windows 资源管理器的视图,一般大纲如下所示:

<ListView>
    <ListView.Resources>
        <DataTemplate x:Key="IconTemplate">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto"/>
                    <ColumnDefinition/>
                </Grid.ColumnDefinitions>
                <Image Grid.Column="0"/>
                <TextBlock Grid.Column="1" Text="{Binding Name}"/>
            </Grid>
        </DataTemplate>
    </ListView.Resources>            
    <ListView.View>     
        <GridView>
            <GridViewColumn CellTemplate="{StaticResource IconTemplate}" Header="Name"/>
            <GridViewColumn DisplayMemberBinding="{Binding Size}" Header="Size"/>
            <GridViewColumn DisplayMemberBinding="{Binding Type}" Header="Type"/>                    
        </GridView>
    </ListView.View>
</ListView>