Question

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.

Was it helpful?

Solution

Well, after a bunch of research, I ended up using a value converter. I made it OneWay so it only converts, but doesn't convert back - 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 helps someone else out.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top