jeudi 2 avril 2015

WPF Checkbox With Select All Binding IsChecked to Object Property

I have created a ComboBox with Checkboxes and a Select All option at the top of it. The problem I am having is that I need to bind the IsChecked property of the checkboxes to the IsSelected property of the objects that the ComboBox is displaying. The way that I have been able to build the Select All checkbox has taken up the binding for the IsChecked property, so I'm not sure how to go about doing this.


Here is the code with the working Select All checkbox, but without the checkbox being bound to the object's IsSelected property:



<ComboBox Canvas.Left="38" Canvas.Top="7" Width="187" IsEnabled="{Binding IsEncounterSelectorEnabled}" >
<ComboBox.Resources>
<CollectionViewSource x:Key="AvailableEncounters" Source="{Binding AvailableEncounters}" />
</ComboBox.Resources>
<ComboBox.ItemsSource>
<CompositeCollection>
<ComboBoxItem>
<CheckBox x:Name="All">All</CheckBox>
</ComboBoxItem>
<CollectionContainer Collection="{Binding Source={StaticResource AvailableEncounters}}" />
</CompositeCollection>
</ComboBox.ItemsSource>
<ComboBox.ItemTemplate>
<DataTemplate>
<CheckBox Content="{Binding Date}" IsChecked="{Binding ElementName=All, Path=IsChecked, Mode=OneWay}" />
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>


Alternatively, here is the Combo Box with a broken "Select All" option, but that is properly bound to IsSelected:



<ComboBox Canvas.Left="38" Canvas.Top="7" Width="187" IsEnabled="{Binding IsEncounterSelectorEnabled}" >
<ComboBox.Resources>
<CollectionViewSource x:Key="AvailableEncounters" Source="{Binding AvailableEncounters}" />
</ComboBox.Resources>
<ComboBox.ItemsSource>
<CompositeCollection>
<ComboBoxItem>
<CheckBox x:Name="All">All</CheckBox>
</ComboBoxItem>
<CollectionContainer Collection="{Binding Source={StaticResource AvailableEncounters}}" />
</CompositeCollection>
</ComboBox.ItemsSource>
<ComboBox.ItemTemplate>
<DataTemplate>
<CheckBox Content="{Binding Date}" IsChecked="{Binding IsSelected}" />
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>


So I'm basically trying to find a way to get both of these bindings on the same checkbox:



<CheckBox Content="{Binding Date}" IsChecked="{Binding ElementName=All, Path=IsChecked, Mode=OneWay}" />
<CheckBox Content="{Binding Date}" IsChecked="{Binding IsSelected}" />




Aucun commentaire:

Enregistrer un commentaire