简体   繁体   English

具有复选框的WPF组合框被选中为与内容不同的源(MVVM)

[英]WPF Combobox with Checkbox checked to different source than content (MVVM)

I have the following View in my application: 我的应用程序中具有以下视图:

<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>

The two itemsource Bindings bind to two Lists of the following types: 这两个itemsource绑定绑定到以下类型的两个列表:

Country class: 国家/地区类别:

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

        public Country(string name)
        {
            Name = name;
        }
    }

Clients is also another list in my ViewModel 客户也是我的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;
        }
    }

My problem: Im trying to find a way to check the checkboxes in the view if the client has the same country in his list as the checkbox displays. 我的问题:我试图找到一种方法来检查视图中的复选框,如果客户列表中的客户与复选框所显示的国家/地区相同。

Example: View displays countries from ViewModel.Countries 'switzerland', 'germany' and 'austria' and the ViewModel.SelectedClient.Countries contrains 'switzerland' and 'austria' then only those two should be checked on the view . 示例: ViewViewModel.Countries ','德国'和'austria'和ViewModel.SelectedClient.Countries约束'switzerland'和'austria'来显示国家,那么仅应在view上检查这两个国家。

 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM