2016-11-03 13 views
1

プロパティを変更する方法IsCheckedプロパティに応じて、チェックボックスで現在選択されている要素のアイコンを埋めますか?WPFでdatatemplateでトリガーを使用する方法

マイリソースディクショナリ:

<UserControl.Resources> 
    <ResourceDictionary> 
     <ResourceDictionary.MergedDictionaries> 
      <!-- (...) --> 
     </ResourceDictionary.MergedDictionaries> 
     <DataTemplate x:Key="FooTemplate"> 
      <Icons:ExIcon Fill="Red" 
          Width="12" 
          Height="Auto"> 
       <!-- 
       <Icons:ExIcon.Triggers> 
        <DataTrigger Binding="{Binding RelativeSource={???}}, Path=???}" Maybe here? 
         Value="???"/> 
       </Icons:ExIcon.Triggers> 
       --> 
      </Icons:ExIcon> 
     </DataTemplate> 
     <!-- (...) --> 
     <styles:IconSelector x:Key="IconSelector" 
          FooTemplate="{StaticResource FooTemplate}" 
          FooTemplateSSecond="{StaticResource FooTemaplteSecond}"/> 

そして、リストボックス:

<ListBox ItemsSource="{Binding DataSources}"> 
    <ListBox.ItemTemplate> 
     <DataTemplate> 
      <StackPanel Orientation="Horizontal"> 
       <!-- (...) --> 
       <CheckBox IsChecked="{Binding IsSelected, Mode=TwoWay}"> 
        <CheckBox.Template> 
         <!-- (...) --> 
         <ContentControl Name="Icon" 
             Content="{Binding}" 
             ContentTemplateSelector="{StaticResource IconSelector}" 
             HorizontalAlignment="Right" 
             Grid.Column="1"/> 
         <!-- (...) --> 
        </CheckBox.Template> 
       </CheckBox> 
      </StackPanel> 
     </DataTemplate> 
    </ListBox.ItemTemplate> 
</ListBox> 

が、それは可能ですか?

答えて

1

あなたのCheckBox.Templateの外観は完全にわかりません。また、Icons:ExIconコントロールは私にとって謎です。とにかくここに小さな実例があります。 ExIconではなくRectangleを使用します。しかし、あなたは簡単にそれを置き換えることができます;-)私はIsSelectedDataTriggerを介してElementNameまたはRelativeSourceの代わりに直接バインドしました。

XAML:

 <ListBox ItemsSource="{Binding}"> 
     <ListBox.Resources> 
      <DataTemplate x:Key="template1"> 
       <StackPanel Orientation="Horizontal"> 
        <CheckBox IsChecked="{Binding Path=IsSelected}" /> 
        <Rectangle Height="20" 
           Width="20"> 
         <Rectangle.Style> 
          <Style TargetType="Rectangle"> 
           <Setter Property="Fill" 
             Value="Blue" /> 
           <Style.Triggers> 
            <DataTrigger Binding="{Binding Path=IsSelected}" 
               Value="True"> 
             <Setter Property="Fill" 
               Value="Red" /> 
            </DataTrigger> 
           </Style.Triggers> 
          </Style> 
         </Rectangle.Style> 
        </Rectangle> 
       </StackPanel> 
      </DataTemplate> 
     </ListBox.Resources> 
     <ListBox.ItemTemplate> 
      <DataTemplate> 
       <ContentControl ContentTemplate="{StaticResource template1}" Content="{Binding}"> 

       </ContentControl> 
      </DataTemplate> 
     </ListBox.ItemTemplate> 
    </ListBox> 

NOTEContent="{Binding}"。これにより、ContentPresenterDataContextListBoxItemの「実際の」DataContextに設定されます。あなたはVS2015を使用する場合は、そうでなければ、あなたはまた、IsCheckedIsSelectedの名前を変更するために検討すべきであるhttps://snoopwpf.codeplex.com/

使用することができますVisualTreeViewerでこれを調査することができます。また、IsSelectedプロパティを使用して、行が選択されているかどうかを示します。

+0

不幸にも、これで問題は解決しません。リストの要素の種類によって、異なるテンプレートが適用されます。テンプレートはResourceDictionaryで定義する必要があります。 例:リストには従業員が表示されます。従業員がマネージャーの場合、「A」アイコンが表示されます。従業員がディレクターの場合、「B」アイコン...と表示されます。 アイテムがチェックされている場合、ContentControlの子の色を変更したい場合があります(時には "A"、時には "B")。 – WymyslonyNick

+0

アイコンはContentControlのコンテンツであることが重要です。 – WymyslonyNick

+0

異なるViewModel ''に対して、異なるDataTemplatesを使用できます。次に、ContentTemplateSelectorは必要ありません。 DatatemplateはItemTemplateとして自動的に適用されます。 http://stackoverflow.com/questions/19864891/wpf-mvvm-why-use-contentcontrol-datatemplate-views-rather-than-straight-xaml-wも参照してください。とにかく私はContentControlに例を更新します – Mat

関連する問題