2016-01-21 60 views
0

当多个DataTriggers绑定到同一个ViewModel属性时,我遇到了一个问题。经过调查,我注意到总是最后一个胜利。我怎样才能克服这个问题?所以多个DataTrigger绑定到同一个VM属性的问题

   <DataTrigger Binding="{Binding Path=ShowGrid}" Value="false"> 
 
        <Setter Property="Height" Value="0"></Setter> 
 
       </DataTrigger> 
 

 
       <DataTrigger Binding="{Binding Path=ShowGrid}" Value="true"> 
 
        <Setter Property="Height" Value="100"></Setter> 
 
       </DataTrigger>

,它一定是在故事情节有关,任何蛛丝马迹:谢谢

  <Style.Triggers> 
 

 
       <DataTrigger Binding="{Binding Path=ShowGrid}" Value="false"> 
 
        <DataTrigger.EnterActions> 
 
         <BeginStoryboard> 
 
          <Storyboard> 
 
           <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Height)"> 
 
            <EasingDoubleKeyFrame KeyTime="0" Value="50"/> 
 
            <EasingDoubleKeyFrame KeyTime="0:0:0.2" Value="0"/> 
 
           </DoubleAnimationUsingKeyFrames> 
 
          </Storyboard> 
 
         </BeginStoryboard> 
 
        </DataTrigger.EnterActions> 
 
       </DataTrigger> 
 

 
       <DataTrigger Binding="{Binding Path=ShowGrid}" Value="true"> 
 
        <DataTrigger.EnterActions> 
 
         <BeginStoryboard> 
 
          <Storyboard> 
 
           <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Height)"> 
 
            <EasingDoubleKeyFrame KeyTime="0" Value="0"/> 
 
            <EasingDoubleKeyFrame KeyTime="0:0:0.2" Value="50"/> 
 
           </DoubleAnimationUsingKeyFrames> 
 
          </Storyboard> 
 
         </BeginStoryboard> 
 
        </DataTrigger.EnterActions> 
 
       </DataTrigger> 
 

 
      </Style.Triggers>

顺便说一句,这工作得很好?

回答

0

故事板的FillBehavior默认为HoldEnd,将其更改为FillBehavior=Stop

的默认行为是保持故事板集的最后的值,告诉它重置他们也就完成了。

编辑

好了,现在我明白你想要什么。你必须使用DataTrigger.ExitActions:

<DataTrigger Binding="{Binding Path=ShowGrid}" Value="true"> 
    <DataTrigger.EnterActions> 
     <BeginStoryboard> 
      <Storyboard> 
       <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Height)"> 
        <EasingDoubleKeyFrame KeyTime="0" Value="0"/> 
        <EasingDoubleKeyFrame KeyTime="0:0:0.2" Value="50"/> 
       </DoubleAnimationUsingKeyFrames> 
      </Storyboard> 
     </BeginStoryboard> 
    </DataTrigger.EnterActions> 
    <DataTrigger.ExitActions> 
     <BeginStoryboard> 
      <Storyboard> 
       <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Height)"> 
        <EasingDoubleKeyFrame KeyTime="0" Value="50"/> 
        <EasingDoubleKeyFrame KeyTime="0:0:0.2" Value="0"/> 
       </DoubleAnimationUsingKeyFrames> 
      </Storyboard> 
     </BeginStoryboard> 
    </DataTrigger.ExitActions> 
</DataTrigger> 

只是试过了,它似乎做你想做的。不要忘记删除其他DataTrigger。

+0

嘿,谢谢你的回答。尽管您的建议部分有效(不是预期的行为),但这并不能解决我的问题。我有两个按钮(显示和隐藏)。一旦点击了“显示”,命令正在执行,ViewModel中的属性将被更改,然后执行DataTrigger(向下滑动网格并保持)。同样的情况发生在另一个按钮上,显然布尔属性设置为相反的值(滑回网格并保持)。我很接近,但仍然不在那里。还有其他建议吗?谢谢 – lucas

+0

我能够实现预期行为的唯一方法是将事件附加到两个按钮上,并且在后面的代码中的每个事件都会更新网格的高度。这工作正常,但我想完全用XAML来完成,有没有办法。谢谢。值得注意的是,之前提到的'FillBehavior = Stop'在这里起作用非常重要。 – lucas

+0

我已经找到解决上述问题的解决方案并不是最佳选择。它无法在一种情况下工作。需要弄清楚如何使它与纯XAML工作 – lucas