.net ListBox 和 ListView 有什么区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4703641/
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-09-03 15:08:09  来源:igfitidea点击:

What is The difference between ListBox and ListView

.netwpfwpf-controls

提问by Rasto

What is the difference between WPF's ListBox and ListView? I can not find any significant difference in their properties. Is there different typical use?

WPF 的 ListBox 和 ListView 有什么区别?我找不到它们的属性有任何显着差异。有不同的典型用途吗?

回答by Thomas Levesque

A ListViewis basically like a ListBox(and inherits from it), but it also has a Viewproperty. This property allows you to specify a predefined way of displaying the items. The only predefined view in the BCL (Base Class Library) is GridView, but you can easily create your own.

AListView基本上类似于 a ListBox(并继承自它),但它也有一个View属性。此属性允许您指定显示项目的预定义方式。BCL(基类库)中唯一的预定义视图是GridView,但您可以轻松创建自己的.

Another difference is the default selection mode: it's Singlefor a ListBox, but Extendedfor a ListView

另一个区别是默认选择模式:它Single用于 a ListBox,但Extended用于 aListView

回答by Kylo Ren

A ListViewlet you define a set of viewsfor it and gives you a native way (WPFbindingsupport) to control the display of ListViewby using defined views.

AListView允许您views为它定义一组 ,并为您提供一种本地方式(WPFbinding支持)来控制ListView使用 defined的显示views

Example:

例子:

XAML

XAML

<ListView ItemsSource="{Binding list}" Name="listv" MouseEnter="listv_MouseEnter" MouseLeave="listv_MouseLeave">
        <ListView.Resources>
            <GridView x:Key="one">
                <GridViewColumn Header="ID" >
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding id}" />
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
                <GridViewColumn Header="Name" >
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding name}" />
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
            </GridView>
            <GridView x:Key="two">                    
                <GridViewColumn Header="Name" >
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding name}" />
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
            </GridView>
        </ListView.Resources>
        <ListView.Style>
            <Style TargetType="ListView">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding ViewType}" Value="1">
                        <Setter Property="View" Value="{StaticResource one}" />
                    </DataTrigger>
                </Style.Triggers>
                <Setter Property="View" Value="{StaticResource two}" />
            </Style>
        </ListView.Style>  

Code Behind:

Code Behind:

private int viewType;

public int ViewType
{
    get { return viewType; }
    set 
    { 
        viewType = value;
        UpdateProperty("ViewType");
    }
}        

private void listv_MouseEnter(object sender, MouseEventArgs e)
{
    ViewType = 1;
}

private void listv_MouseLeave(object sender, MouseEventArgs e)
{
    ViewType = 2;
}

OUTPUT:

输出:

Normal View: View 2 in above XAML

普通视图:上面的视图 2 XAML

Normal

普通的

MouseOver View: View 1 in above XAML

鼠标悬停视图:查看上方的 1 XAML

Mouse Over

鼠标移到

If you try to achieve above in a ListBox, probably you'll end up writing a lot more code forControlTempalate/ItemTemplateof ListBox.

如果您尝试在 a 中实现上述目标ListBox,则可能最终会为ControlTempalate/ ItemTemplateof编写更多代码ListBox

回答by iaminvinicble

Listview derives from listbox control. One most important difference is listview uses the extended selection mode by default . listview also adds a property called view which enables you to customize the view in a richer way than a custom itemspanel. One real life example of listview with gridview is file explorer's details view. Listview with grid view is a less powerful data grid. After the introduction of datagrid control listview lost its importance.

Listview 派生自列表框控件。一个最重要的区别是 listview 默认使用扩展选择模式。listview 还添加了一个名为 view 的属性,它使您能够以比自定义 itempanel 更丰富的方式自定义视图。带有 gridview 的列表视图的一个真实示例是文件资源管理器的详细信息视图。带有网格视图的 Listview 是一种功能较弱的数据网格。引入datagrid控件后listview失去了重要性。