Reputation:
I have a listbox
control. I have created an ItemTemplate
for this listbox
which has two controls within it.
Structure
<Listbox x:Key="Listbox1" ItemTemplate={StaticResource ListBox1ParentItemTemplate}>
</Listbox>
<Datatemplate x:Key="ListBox1ParentItemTemplate">
<ToggleButton></ToggleButton>
<Listbox x:Key="Listbox1" ItemTemplate={StaticResource ListBox2ParentItemTemplate}>
</Listbox>
</Datatemplate>
<Datatemplate x:Key="ListBox2ParentItemTemplate">
<TextBlock Text="{Binding Mode=Default, XPath=@Description}" TextWrapping="Wrap"/>
<CheckBox DockPanel.Dock="Right" />
</Datatemplate>
Basically the Listbox2
is a CheckedListbox
. My requirement is as follows
1. Bind the ToggleButton
to Listbox1 SelectedItem
i.e if 4 item is selected in Listbox1
then the togglebutton of 4th item should get checked automatically and other togglebutton should get unchecked.
2. If the ToggleButton
is checked then all checkboxes in Listbox2
should automatically selected. Also if one of the checkboxes in Listbox2
is unselected then ToogleButton
should get unchecked automatically. (This functionality is similar to TreeNode
and childnode functionality. If parentnode is checked then all childnodes get selected and if one of the childnode is unselected parentnode should get unselected).
Upvotes: 1
Views: 322
Reputation: 50028
Sample code will help us understand your problem better.
Looks like you are trying to add some business logic on to the UI, Try creating proper ViewModel and then Bind it to the control so that the calculations can be done at the ViewModel side and based on your logic you can update the UI- (INotifyPropertyChanged)
The ViewModel will be having a Bool property(Bind to ToggleButton) which changes according to the other 3 bool(which binds to the 4 Checkboxes). At any setter call inside the ViewModel has to recalculate(It will be a boolean AND operation) the entire Properties again. Since CheckBox binding mode is defaulted to TwoWay, it will be very easy to implement.
Upvotes: 1