在listview中绑定多个源

时间:2015-04-12 07:47:41

标签: xaml windows-runtime winrt-xaml

我正在尝试使用WINRT学习XAML,我对绑定和视图模型有疑问。

我有两个类EmployeeList和DownloadableEmployee:

public sealed class EmployeeList
{
public IReadOnlyList<DownloadableEmployee> DownloadableEmployees { get; }

}

public sealed class DownloadableEmployee
{
public System.Boolean IsSelected { get; set; }
public System.String Name { get; }

//method
public IAsyncOperation<IRandomAccessStreamWithContentType> GetEmployeePicAsync();
}

我已将EmployeeList绑定到显示员工姓名和图片的 Multi Selectable ListView 。用户现在可以在ListView中选择/取消选择单个员工。

为实现这一目标,我已经创建了一个充当ViewModel的DownloadableItem类,该类绑定到我的listview EmployeeLV。

public class DownloadableItemVM
    {
        public string Name
        {
            get;
            private set;
        }

        public IRandomAccessStreamWithContentType EnmployeePic
        {
            get;
            private set;
        }

      public DownloadableItem(string name, IRandomAccessStreamWithContentType     thumbnail)
        {
            Name = name;
            Thumbnail = thumbnail;
        }
}

 ObservableCollection<DownloadableItemVM> employeesToDownload = new ObservableCollection<DownloadableItemVM>();

// set source to ListView
EmployeeLV.ItemsSource = employeesToDownload;

foreach (DownloadableEmployee item in EmployeeList.DownloadableEmployees)
                {
                    IRandomAccessStreamWithContentType stream = await item.GetEmployeePicAsync();                    
                    employeesToDownload.Add(new DownloadableItemVM(item.Name, stream));
                }

我的列表视图

 <ListView x:Name="employeeLV" SelectionMode="Multiple" >
        <ListView.ItemTemplate>
            <DataTemplate>
                <StackPanel>
                    <Image Source="{Binding Thumbnail}"/>
                    <TextBlock Text="{Binding Name}"/>
                </StackPanel>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>      

问题XAML专家:

我的ListView已经绑定到我的自定义View Model类DownloadableItemVM并正确显示每个员工的姓名和照片。

但是现在我遇到了问题,因为我需要&#34;绑定&#34;我的ListView中的每个选定项目都返回到DownloadableEmployee.IsSelected,这样当用户选择一个员工时,他们的&#34; IsSelected&#34;当他们取消选择时,设置为true,反之亦然。

我该怎么做呢?

1 个答案:

答案 0 :(得分:0)

为什么不在DownloadableItemVM中包含所有DownloadableEmployee对象,并通过此对象绑定到名称,图片等? E.g。

public class DownloadableItemVM
{
    ...
    public DownloadableEmployee Employee
    {
        get;
        private set;
    }
    ...
    public DownloadableItem(DownloadableEmployee employee, IRandomAccessStreamWithContentType thumbnail)
    {
        Employee= employee;
        Thumbnail = thumbnail;
    }
    ...
}

 <ListView x:Name="employeeLV" SelectionMode="Multiple" >
    <ListView.ItemTemplate>
        <DataTemplate>
            <StackPanel>
                <Image Source="{Binding Thumbnail}"/>
                <TextBlock Text="{Binding Employee.Name}"/>
            </StackPanel>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>      

使用此方法,您可以通过Employee对象访问IsSelected prop。这适合你吗?