我搜索了這個主題並找到了幾個討論。 我不喜歡那些實現是他們要麼隱含特殊的綁定數據(與IsSelected屬性)或缺乏正常的鍵盤支持。選擇/取消選中XAML中「CheckListBox」中的所有複選框
列表中的複選框更多的是裝飾性的東西,而不是功能性的擴展,因此應該對其進行相應的處理。
我從這個好的和有用的文章開始:http://www.gbogea.com/2010/01/02/mvvm-multiselect-listbox
但我beleive贊成視圖模型的黑客查看不太好做法。它的ViewModel適用於視圖和模型。當然,恕我直言。
因此,我改變一點點,這個XAML完成:
<ListBox Name="checkboxList"
ItemsSource="{Binding Sports}"
Margin="0,5"
SelectionMode="Multiple">
<ListBox.ItemContainerStyle>
<Style>
<Setter Property="ListBoxItem.Background" Value="Transparent"/>
<Setter Property="ListBoxItem.Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBoxItem}">
<Border x:Name="Bd"
SnapsToDevicePixels="true"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
Padding="{TemplateBinding Padding}">
<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ListBox.ItemContainerStyle>
<ListBox.ItemTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ListBoxItem}, Path=IsSelected, Mode=TwoWay}"
Content="{Binding}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
這裏選中複選框處於同步與ListBoxItem.IsSelected和列表框不知道綁定數據特殊性。
這是好的。 但是有什麼辦法通過添加一個複選框到控件模板來爲ListBox添加Select | Deselect All?僅使用XAML。
問題是? – vulkanino 2012-02-21 14:06:56
呃..忘了問題=)對不起。 – 2012-02-21 14:13:40