【发布时间】:2018-06-13 05:20:10
【问题描述】:
对不起,如果我的问题的措辞不是很好,或者如果这已经在某个地方得到了回答,我已经搜索过了,但我真的不知道如何解释我想要做什么。
这是一个简单的测试平台,我已经部分设置以帮助解释:
MainWindow.xaml:
<Window x:Class="wpfExample.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:wpfExample"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<ListView Grid.Column="0" ItemsSource="{Binding People}" DisplayMemberPath="Name"/>
<ListBox Grid.Column="1" ItemsSource="{Binding Interests}" Margin="0,4,4,4">
<ListBox.ItemTemplate>
<DataTemplate>
<CheckBox Content="{Binding Name}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
MainWindow.xaml.cs:
using System;
using System.Collections.ObjectModel;
using System.Windows;
namespace wpfExample
{
public class Person
{
public string Name { get; set; }
public ObservableCollection<Guid> Interests { get; set; }
}
public class Interest
{
public Guid Id { get; set; }
public string Name { get; set; }
}
public class Data
{
public ObservableCollection<Person> People { get; set; }
public ObservableCollection<Interest> Interests { get; set; }
}
public partial class MainWindow : Window
{
public MainWindow()
{
DataContext = new Data
{
People = new ObservableCollection<Person>
{
new Person {Name="Fred", Interests=new ObservableCollection<Guid>() },
new Person {Name="Jane", Interests=new ObservableCollection<Guid>() },
new Person {Name="Zach", Interests=new ObservableCollection<Guid>() }
},
Interests = new ObservableCollection<Interest>
{
new Interest {Name="Gardening", Id=Guid.NewGuid() },
new Interest {Name="Writing", Id=Guid.NewGuid() },
new Interest {Name="Avoiding Tax", Id=Guid.NewGuid() }
}
};
InitializeComponent();
}
}
}
所以我有一个包含两个列表的 DataContext。一个包含有名称和 ID 的兴趣。另一个包含具有姓名和兴趣 ID 列表的人员。
当在 UI 中选择了一个人时,我希望能够在他们各自的列表中添加和删除兴趣 ID,因此第 1 列中的 ListView 绑定到兴趣列表,但是我如何正确绑定列表中复选框的 IsChecked 属性?
在我的完整项目中,我已经能够成功读取所选人员的兴趣列表的属性,方法是使用 IsChecked 的 MultiBinding 和 MultiValueConverter 一起传递兴趣 ID 和人员的兴趣列表(因为您不能将参数绑定用于“正常”值转换器)。我觉得这个解决方案有点滥用转换器,但如果有必要我很乐意坚持下去。
当复选框被切换时,如何实现允许我在个人兴趣列表中添加和删除兴趣指南的系统?有更清洁的方法吗?如果可以避免,我不想更改模型。
【问题讨论】: