0
我在我的应用程序如下观点:WPF的组合框复选框检查,以不同的源大于内容(MVVM)
<ComboBox x:Name="comboBox" HorizontalAlignment="Left" Margin="87,53,0,0" VerticalAlignment="Top" Width="182" ItemsSource="{Binding Clients}" DisplayMemberPath="Email" SelectedItem="{Binding SelectedClient}"/>
<ListBox x:Name="listBox" HorizontalAlignment="Left" Height="100" Margin="87,98,0,0" VerticalAlignment="Top" Width="182" ItemsSource="{Binding Countries}">
<ListBox.ItemTemplate>
<DataTemplate>
<CheckBox Content="{Binding Name}" Checked=""/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
两个的ItemSource绑定绑定到以下类型的两个列表:
国家类:
public class Country
{
public Guid Guid { get; set; } = Guid.NewGuid();
public string Name { get; set; }
public Country(string name)
{
Name = name;
}
}
客户也是我的ViewModel另一个列表
public class Client
{
public Guid Guid { get; set; } = Guid.NewGuid();
public string Prename { get; set; }
public string Lastname { get; set; }
public string Email { get; set; }
public List<Country> Countries { get; set; } = new List<Country>();
public List<AreaType> Areas { get; set; } = new List<AreaType>();
public Client() { }
public Client(string prename, string lastname, string email, List<Country> countires = null, List<AreaType> areas = null)
{
Prename = prename;
Lastname = lastname;
Email = email;
Countries = countries;
Areas = areas;
}
}
我的问题:我试图找到一种方法来检查视图中的复选框,如果客户端在复选框显示的列表中具有相同的国家/地区。
实施例:View
显示从ViewModel.Countries
“瑞士”,“德国”和“奥地利”和ViewModel.SelectedClient.Countries
contrains“瑞士”和“奥地利”国家然后只有这两个应该在view
检查。
也许你可以添加布尔到国家“选定”。在创建客户时,请将您国家/地区列表中可用的国家/地区设置为selected = true。然后在xaml中,您可以将Selected字段绑定到您的复选框。 –
@JeroenDop这将工作我猜虽然我不认为它是一个非常好的想法,添加一个属性的模型只是为了能够检查一些复选框,因为这不是国家类的唯一用法。 Imo可能用与国家本身无关的数据“损坏”模型 – VIP
确实,您可以添加具有添加属性的ViewModel ClientCountry。将ClientCountry添加到客户端的国家/地区列表中。 –