带有Checkbox的WPF Combobox检查到的内容不同于内容(MVVM)

时间:2016-04-20 08:59:19

标签: c# wpf checkbox 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禁区'瑞士'和'奥地利'的国家/地区只有那两个应该是检查view

1 个答案:

答案 0 :(得分:0)

 public class Country
        {
            public Guid Guid { get; set; } = Guid.NewGuid();
            public string Name { get; set; }
            public bool Selected{get;set;}
            public Country(string name)
            {
                Name = name;
            }
        }

           <DataTemplate>
                    <CheckBox Content="{Binding Name}" Checked="{Binding Selected}"/>
           </DataTemplate>