本文介绍了将Dictionary(其中Value是用户类)绑定到ListView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我有一个可观察的字典,其中包含字符串Key和Contact(我的用户类)值.
我想将我的联系人绑定到ListView.
我想编写一个显示联系人姓名,电子邮件和图像的数据模板.
在将字典绑定到ListView上时,我需要您的帮助.

谢谢.


Janiv Ratson.技术和开发专家.| BL:http://blogs.microsoft.co.il/blogs/janiv/|李:http://www.linkedin.com/in/janiv| TW:http://twitter.com/janiv

Hello,

I have an observable dictioary containing a string Key and a Contact (my user class) value.
I want to bind my contacts to a ListView.
I want to write a DataTemplate that will display the Contact Name, eMail and Image.
I need your help on binding the dictionary to a ListView.

Thank you.


Janiv Ratson.Technology and Development Expert.| BL: http://blogs.microsoft.co.il/blogs/janiv/ | LI: http://www.linkedin.com/in/janiv| TW: http://twitter.com/janiv

推荐答案

 <Grid>
        <ListView Margin="30,52,34,57" Name="ListView1" >
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="Name" DisplayMemberBinding="{Binding Value.Name}"/>
                    <GridViewColumn Header="Mail" DisplayMemberBinding="{Binding Value.Email}"/>
                </GridView> 
            </ListView.View>  
        </ListView>
    </Grid>


隐藏代码:


Code Behind:

Imports System.Windows.Forms
Imports System.Collections.ObjectModel

Class Window1

    Private Sub Window1_Loaded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded
        Dim dictValues As New Dictionary(Of String, MyData)
        Dim objData As New MyData
        objData.Name = "Name1"
        objData.Email = "Mail1"
        dictValues.Add("1", objData)

        objData = New MyData
        objData.Name = "Name2"
        objData.Email = "Mail2"
        dictValues.Add("2", objData)

        objData = New MyData
        objData.Name = "Name3"
        objData.Email = "Mail3"
        dictValues.Add("3", objData)

        ListView1.ItemsSource = dictValues

    End Sub
End Class

Class MyData

    Private strEmail As String
    Public Property Email() As String
        Get
            Return strEmail
        End Get
        Set(ByVal value As String)
            strEmail = value
        End Set
    End Property

    Private strName As String
    Public Property Name() As String
        Get
            Return strName
        End Get
        Set(ByVal value As String)
            strName = value
        End Set
    End Property

End Class


希望对您有帮助


Hope it helps


这篇关于将Dictionary(其中Value是用户类)绑定到ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 02:15