Using Listbox in ItemTemplate

I have a control listbox

. I created ItemTemplate

for this listbox

which has two controls.

  • Switch button
  • Listbox management

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 it Listbox2

is CheckedListbox

. My requirement is the following 1. Bind ToggleButton

to Listbox1 SelectedItem

ie, if Listbox1

4 elements are selected in an element, then the switch of the 4th element should be checked automatically and the other switch should be unchecked. 2. If checked ToggleButton

, then all checkboxes in Listbox2

should be automatically selected. In addition, if one of the checkboxes is Listbox2

not selected, then ToogleButton

it should be automatically disabled. (This functionality is similar to that of TreeNode

childnode. If parentnode is checked, all child elements are selected and if one of the child nodes is not selected, the parent class should be overridden).

+1


source to share


1 answer


The sample code will help us better understand your problem.

It looks like you are trying to add some business logic to the UI, try to create the correct ViewModel and then bind it to the control so the calculations can be done on the ViewModel side and based on your logic you can update the interface - (INotifyPropertyChanged)



The ViewModel will have a Bool (Bind to ToggleButton) property that changes to match the other 3 bools (which binds to 4 checkboxes). Any time the setter is called inside the ViewModel, all properties must be recalculated (it will be a logical AND operation) again. Since the default binding mode of a CheckBox is set to TwoWay, it will be very easy to implement.

+1


source







All Articles