2012-02-21 20 views
0

このトピックを検索し、いくつかのディスカッションが見つかりました。 私はこれらの実装について気に入らなかったのは、特別な種類のバインドされたデータ(IsSelectedプロパティ)を暗示しているか、通常のキーボードサポートが不足していたことです。XAMLの "CheckListBox"内のすべてのCheckBoxを選択/選択解除する

リストのCheckBoxは、機能的な拡張ではなく装飾的なものであるため、それに応じて処理する必要があります。

私はこの素敵で親切記事から始まっ:http://www.gbogea.com/2010/01/02/mvvm-multiselect-listbox

しかし、私はViewModelにの賛成でビューをハッキング信じかなり良い習慣ではありません。それはViewModelをViewとModelに適応させるものです。確かに、IMHO。

だから私は少し変更して、この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やリストボックスと同期しているがバインドされたデータの特殊性を認識していません。

これはOKです。 しかし、コントロールテンプレートにもう1つのチェックボックスを追加することで、リストボックスのすべての選択/選択解除を追加する方法はありますか? XAMLのみを使用する。

+0

質問は? – vulkanino

+0

えー..質問を忘れた=)申し訳ありません。 –

答えて

1

あなたはこのしかし、あなたが言及するとき、またはそのCheckBoxの状態が変更されない、すなわちことを、考えていなかった問題を解決していませんBlend SDK

<ListBox ItemsSource="{Binding Data}" SelectionMode="Extended"> 
    <ListBox.Template> 
     <ControlTemplate TargetType="ListBox"> 
      <StackPanel> 
       <CheckBox Content="All"> 
        <i:Interaction.Triggers> 
         <i:EventTrigger EventName="Checked"> 
          <is:CallMethodAction MethodName="SelectAll" 
            TargetObject="{Binding RelativeSource={RelativeSource TemplatedParent}}" /> 
         </i:EventTrigger> 
         <i:EventTrigger EventName="Unchecked"> 
          <is:CallMethodAction MethodName="UnselectAll" 
            TargetObject="{Binding RelativeSource={RelativeSource TemplatedParent}}" /> 
         </i:EventTrigger> 
        </i:Interaction.Triggers> 
       </CheckBox> 
       <ItemsPresenter /> 
      </StackPanel> 
     </ControlTemplate> 
    </ListBox.Template> 
</ListBox> 

から例Interactivityのために使用してそのような何かを行うことができます選択を使用せずに変更されます。代わりにMultiBindingと値コンバーターを使用してIsChecked状態をバインドして、正しい状態にすることができます。その後、イベントをドロップすることもできます。それはまたむしろ面倒です。

関連する問題