mercredi 20 avril 2016

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:

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

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.




Aucun commentaire:

Enregistrer un commentaire