使用DataTrigger来代替Triggerr

普通的Trigger监听鼠标移入的代码如下:

  <Trigger Property="IsMouseOver" Value="true">

      <Setter .../>

  </Trigger>

而MultiTigger想实现,当属性等于某个值,同时鼠标移入时的需求:

    理论上,代码应该如下:

<MultiTrigger>
    <MultiTrigger.Conditions>
        <Condition Property="IsMouseOver" Value="True"/>
        <Condition Binding="{Binding RelativeSource={RelativeSource Mode=Self}, Path=IsChecked}" Value="False"/>
    </MultiTrigger.Conditions>
    <MultiTrigger.EnterActions>
        <BeginStoryboard Storyboard="{StaticResource MouseEnter}"/>
    </MultiTrigger.EnterActions>
    <MultiTrigger.ExitActions>
        <BeginStoryboard Storyboard="{StaticResource MouseLeave}"/>
    </MultiTrigger.ExitActions>
</MultiTrigger>

  但实际运行起来会报如下错误Must have non-null value for 'Property'.

 

  所以我们需要转换一种方式,用MultiDataTrigger来实现上述的需求:

   代码如下:

    <MultiDataTrigger>
                <MultiDataTrigger.Conditions>
                    <Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsMouseOver}" Value="True"/>
                    <Condition Binding="{Binding RelativeSource={RelativeSource Mode=Self}, Path=IsChecked}" Value="True"/>
                </MultiDataTrigger.Conditions>
                <Setter TargetName="LayoutRoot" Property="Background" Value="Red"/>
            </MultiDataTrigger>

**将Trigger中的
 <Condition Property="IsMouseOver" Value="True"/> 转换成DataTrigger中的
<Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsMouseOver}" Value="True"/>
来实现我们的需求

 

posted @ 2017-03-03 15:16  一菲聪天  阅读(796)  评论(0编辑  收藏  举报