views:

36

answers:

1

I have a ListBox containing CheckBoxes. Xaml looks like this:

<ListBox x:Name="lbContactTypes">
    <ListBox.ItemTemplate>
        <HierarchicalDataTemplate>
            <CheckBox Content="{Binding Path=Description}" IsChecked="{Binding Path=ContactTypes, Converter={x:Static Classes:ListContainsConverter.Instance}, ConverterParameter=1}" />
        </HierarchicalDataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

I bind ItemsSource in my code like this:

lbContactTypes.ItemsSource = GetLeadContactTypes();

So far so good. Three checkboxes are generated with correct content. Now I'd like to set IsChecked property to some other object which holds the current data for the entity I'm working with.

To clarify a bit, I have set DataContext of the window to Customer object which among others contains ObservableCollection of ContactTypes and I would like to bind these ContactTypes to IsChecked property so that checkboxes would be property checked. I check if the ObservableCollection contains the desired integer by calling the ListContainsConverter converter which returns bool. Is this possible?

A: 

I've found a solution to this. I bound IsChecked to Window.DataContext, so the xaml looks like this:

<ListBox x:Name="lbContactTypes">
    <ListBox.ItemTemplate>
        <HierarchicalDataTemplate>
            <CheckBox Content="{Binding Path=Description}" IsChecked="{Binding Path=DataContext.ContactTypes, RelativeSource={RelativeSource AncestorType={x:Type Window}}, Converter={x:Static Classes:ListContainsConverter.Instance}, ConverterParameter=1}" />
        </HierarchicalDataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

I would still be interested to find out if a better solution exist. Let me know.

aks