使用List填充WPF ListView的问题 [英] Problem populating WPF ListView with List

查看:70
本文介绍了使用List填充WPF ListView的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试学习如何将列表绑定到ListView。我的代码填充ListView,但不是把客户端对象的内容重复放入类的名称。 (注意:搜索相关条目但无法掌握可能的解决方案)

WPF数据绑定 - 绑定列表到ListView



(XAML)

 <   ListView     x:名称  =  lvClients < span class =code-attribute>   Horizo​​ntalAlignment   =  Left   高度  =   300    VerticalAlignment   =  Top   宽度  =   300   >  
< ListView.View >
< GridView > ;
< GridViewColumn / >
< / GridView >
< / ListView.View >
< / ListView >





(代码背后)

 List< Client> names =  new  List< Client>(); 

public ucDatabindingListView()
{
InitializeComponent();
names.Add( new 客户端( 1 John));
names.Add( new 客户端( 2 ));

lvClients.ItemsSource = names; // 这不起作用!
}





(另一个c#类)

  public  客户端
{
public int ID { get ; set ; }

public string 名称{获得; set ; }

public 客户端( int id,字符串名称)
{
.ID = id;
.Name = name;
}
}

解决方案

一种可能的解决方案是将GridMemberBinding属性添加到GridViewColumn。每个DisplayMemberBinding属性中的绑定路径应与要在对象中映射的属性相同。每个属性也都有自己的GridViewColumn。



更改此XAML:

 <   ListView     x:名称  =  lvClients    Horizo​​ntalAlignment   =    高度  =  300    VerticalAlignment   =  Top   宽度  =  300   >  
< ListView.View >
< GridView >
< GridViewColumn / >
< / GridView >
< /ListView.View >
< / ListView >





对此:



 <   ListView     x:名称  =  lvClients    Horizo​​ntalAlignment   =    高度  =  300    VerticalAlignment   = 顶部   宽度  =  300   >  
< ListView.View >
< GridView >
< GridViewColumn 标题 = < span class =code-keyword> ID
DisplayMemberBinding = {Binding Path = ID} / >
< GridViewColumn 标题 = 名称 DisplayMemberBinding = {Binding Path = Name} / >
< / GridView >
< / ListView.View >
< / ListView >


Trying to learn how to bind list to ListView . My code populates the ListView but rather than put the contents of the Client object it repeatedly puts the name of the class. (Note: searched for related entries but couldn't grasp the possible solution)
WPF data binding - binding List to ListView

(XAML)

<ListView x:Name="lvClients" HorizontalAlignment="Left" Height="300" VerticalAlignment="Top" Width="300" >
     <ListView.View>
         <GridView>
             <GridViewColumn/>
         </GridView>
     </ListView.View>
</ListView>



(Code behind)

List<Client> names = new List<Client>();

public ucDatabindingListView()
{
     InitializeComponent();
     names.Add(new Client(1, "John"));
     names.Add(new Client(2, "Jane"));

     lvClients.ItemsSource = names; // This doesn't work!
}



(another c# class)

public class Client
 {
     public int ID { get; set; }

     public string Name { get; set; }

     public Client(int id, string name)
     {
         this.ID = id;
         this.Name = name;
     }
 }

解决方案

One possible solution is adding DisplayMemberBinding attribute to GridViewColumn. The Binding Path in each DisplayMemberBinding attribute should be the same name as the property you want to map in the object. Each property also gets its own GridViewColumn.

Changed this XAML:

<ListView x:Name="lvClients" HorizontalAlignment="Left" Height="300" VerticalAlignment="Top" Width="300" >
     <ListView.View>
         <GridView>
             <GridViewColumn/>
         </GridView>
     </ListView.View>
</ListView>



To this:

<ListView x:Name="lvClients" HorizontalAlignment="Left" Height="300" VerticalAlignment="Top" Width="300" >
    <ListView.View>
        <GridView>
            <GridViewColumn Header="ID" DisplayMemberBinding="{Binding Path=ID}"/>
            <GridViewColumn Header="Name" DisplayMemberBinding="{Binding Path=Name}"/>
        </GridView>
    </ListView.View>
</ListView>


这篇关于使用List填充WPF ListView的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆