学习笔记---listbox和listview

本文介绍了ListView和ListBox的区别及其在Windows应用程序中的使用。ListBox允许单选或多选,而ComboBox只有在下拉时显示列表。重点讲解了ListBox的SelectionMode、ScrollIntoView和数据绑定特性,如DisplayMemberPath和SelectedValuePath。接着转向ListView,它是ListBox的扩展,支持Details视图,通过GridView展示多列数据。文中通过示例展示了如何创建GridView、设置列及数据绑定,以及如何使用DataTemplate定制复杂列显示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

     许多控件都派生自ControlControl类,例如:Window、button、ScrollViewer等,他们都有一个Content属性,可以用来添加其他元素。下面要介绍的时Control的另一个分支:ItemsControl。它直接从Control继承。从ItemsControl继承的控件可以显示多个Item。这些控件包括:Menus、Toolbars、statusbars、treeview,listview等。

     Listbox和Combox,TabControl一样,都派生自Selector抽象类。而ListView则派生自ListBox。

Control
       ItemsControl
                 Selector (abstract)
                             ComboBox
                             ListBox

                                   ListView
                             TabControl

     Listbox可以允许用户选择一个和多个Item。ComboBox和LlistBox不同的是,他不会一直显示Items列表,而是只有在下拉的时候才会显示。

     简单介绍ListBox:

     SelectionMode:允许选择一个或多个项目。

     ScrollIntoView:滚动到合适位置,以便选择项可见。

     SelectionChangedEventArgs:此类含有两个属性:AddItems和RemovedItems,在允许多项选择时会用到。

     因为在ListBox中的项都是同一类型,所以我们还可以用到下面三个属性:

     DisplayMemberpath:这是ItemsControl的属性,可以绑定要显示的属性的name。

     SelectedValuePath:来自Selector类。用来设置要表示的Item值的属性的名称。

     SelectedValue:获取由SelectedValuePath表示的Item的属性值。

如:DisplayMemberpath = name;SelectedValuePath= Value;

则在Listbox中界面显示的是Itme的name属性,但我们通过SelectedValue获取的却是Item的Value属性。

     SetBinding和DataContext:数据绑定和上下文。

如:lstbox.SetBinding(ListBox.SelectedValueProperty, "Background");
lstbox.DataContext = this;

表示将listbox的SelecValue属性与this对象的Backgroud属性绑定。

     ListView

     ListBox可以完成大多数显示操作。但是对于Deitails视图来说,因为它需要多列和列标题。在这种情况下就需要ListView控件。Listview直接继承自ListBox。ListView只比ListBox多了一个属性:View(ViewBase类型)。如果View为null,那么ListView就是一个Listbox。

     目前从ViewBase继承的类只有一个GridView;他可以通过多列来表示对象。GridView的核心属性是Columns。每一列都是一个GridViewColumn,它包括列标题、宽度、要表示的Item等。

     下面是ListView的普通用法:首先定义一个要显示的类。然后创建一个ListView绑定该类进行显示。

 


public class SystemParam
    {
        
string strName;
        
object objValue;

        
public string Name
        {
            
set { strName = value; }
            
get { return strName; }
        }
        
public object Value
        {
            
set { objValue = value; }
            
get { return objValue; }
        }
        
public override string ToString()
        {
            
return Name + "=" + Value;
        }
    }

    
class ListSystemParameters : Window
    {
        [STAThread]
        
public static void Main()
        {
            Application app 
= new Application();
            app.Run(
new ListSystemParameters());
        }
        
public ListSystemParameters()
        {
            Title 
= "List System Parameters";

            
// Create a ListView as content of the
 window.
            ListView lstvue 
= new ListView();
            Content 
= lstvue;

            
// Create a GridView as the View of
 the ListView.
            GridView grdvue 
= new GridView();
            lstvue.View 
= grdvue;

            
// Create two GridView columns.
            GridViewColumn col = new GridViewColumn();
            col.Header 
= "Property Name";
            col.Width 
= 200;
            col.DisplayMemberBinding 
= new Binding
(
"Name");
            grdvue.Columns.Add(col);

            col 
= new GridViewColumn();
            col.Header 
= "Value";
            col.Width 
= 200;
            col.DisplayMemberBinding 
= new Binding
(
"Value");
            grdvue.Columns.Add(col);

            
// Get all the system parameters in
 one handy array.
            PropertyInfo[] props 
= typeof
(SystemParameters).GetProperties();

            
// Add the items to the ListView.
            foreach (PropertyInfo prop in props)
                
if (prop.PropertyType != typeof
(ResourceKey))
                {
                    SystemParam sysparam 
= new
 SystemParam();
                    sysparam.Name 
= prop.Name;
                    sysparam.Value 
= prop.GetValue
(
nullnull);
                    lstvue.Items.Add(sysparam);
                }
        }
    }

 

     由于各个列显示的对象类型可能不一样,例如第一列显示text。第二列显示一个combox。在这样的情况下,就需要使用模板。如DataTemplate。然后将模板绑定到GridViewColumn的CellTemplate属性。

如:


// Create DataTemplate for second column.
            DataTemplate template = new
 DataTemplate(
typeof(string));
            FrameworkElementFactory factoryTextBlock 
=
                
new FrameworkElementFactory(typeof
(TextBlock));
            factoryTextBlock.SetValue(TextBlock
.HorizontalAlignmentProperty,
                                     
 HorizontalAlignment.Right);
            factoryTextBlock.SetBinding(TextBlock
.TextProperty,
                                        
new
 Binding(
"Value"));
            template.VisualTree 
= factoryTextBlock;
            col.CellTemplate 
= template;

     在上面将Textblock的text属性绑定到了"Value"属性。所以就不需要再使用DisplayMemberBinding 属性。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值