WPF - How to combine DataTrigger and Trigger?

前端 未结 2 1240
余生分开走
余生分开走 2020-12-24 05:08

NOTE I have asked the related question: How to combine DataTrigger and EventTrigger?

I have a list box containing sever

相关标签:
2条回答
  • 2020-12-24 05:24

    For anyone else who's up against this problem, I found a solution that works for me. Of course, I'm still interested to see other interesting answers.

    Here's what I did:

    <MultiDataTrigger>
      <MultiDataTrigger.Conditions>
        <Condition Binding="{Binding
          RelativeSource={
            RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBoxItem}},
            Path=IsSelected}" Value="True"/>
        <Condition Binding="{Binding IsAvailable}" Value="False"/>
      </MultiDataTrigger.Conditions>
      <Setter TargetName="Name" Property="Foreground" Value="#F00"/>
    </MultiDataTrigger>
    

    There's nothing special about this being a multi trigger though. If you just wanted to style the selected item differently in your data template, you could use:

    <DataTrigger Binding="{Binding 
      RelativeSource={
        RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBoxItem}},
        Path=IsSelected}" Value="True">
      <Setter TargetName="Name" Property="Foreground" Value="#888"/>
    </DataTrigger>
    
    0 讨论(0)
  • 2020-12-24 05:32

    To use it with DataGridRow change binding mode to Self:

    Binding="{Binding RelativeSource={RelativeSource Mode=Self}, Path=... 
    
    0 讨论(0)
提交回复
热议问题