WPF BINDING CHECKBOX CONTENT AND ISCHECKED TO DIFFERENT LISTS

Mark Devid

Total Post:102

Points:714
Posted by  Mark Devid
WPF 
WPF
 3995  View(s)
Ratings:
Rate this:

I have an issue with binding multiple properties to a single control. I have two lists called UserList and GroupList. I'm trying to display a ListView of checkboxes of all the groups in the system. The UserList object contains a list of integers based on the ID of the groups it is a member of. I'd like to have the checkboxes be checked if the UserList object contains an ID of a group in the groupList.

For example, the GroupList might contain IDs 1,2,3,4. While the UserList has a list of IDs of 1 and 3. I'd therefore like checkboxes 1 and 3 checked, but 2 and 4 unchecked.

To do this, I need to bind to two different properties at the same time and possible have some kind of converter. I'm pretty new to this stuff, so I'm kind of stuck as to what to try next. Here's what I've currently got -

<ListView Name="ListBox1"

Grid.Row="1"

Margin="30,4,30,22"

FontFamily="Segoe UI"

FontSize="14"

ItemsSource="{Binding GroupList,

UpdateSourceTrigger=PropertyChanged}">

<ListView.ItemTemplate>

    <DataTemplate>

        <CheckBox Margin="2"

                  Content="{Binding Name}"

                  IsChecked="{Binding Source={StaticResource UserGroupDataProvider}}" />

    </DataTemplate>

</ListView.ItemTemplate>

Obviously I'm missing some kind of static resource, but I'm not sure what to do with that.

Any advice would be appreciated.

  1. Pravesh Singh

    Post:412

    Points:2888
    Re: WPF Binding Checkbox Content and IsChecked to Different Lists

    Hi Mark,

    here's the code I used

        <CheckBox.IsChecked>

            <MultiBinding Converter="{StaticResource CollectionToBoolConverter}" Mode="OneWay">

                <Binding Path="ID" />

                <Binding ElementName="Me" Path="User.GroupList" />

            </MultiBinding>

        </CheckBox.IsChecked>

    Where ElementName=Me is specified up top as the form I'm using.

    Hopefully this will solve your problem.

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!