2010-03-03 88 views
5
<ListBox.ItemTemplate> 
    <DataTemplate> 
     <Grid x:Name="grid"> 
      <Grid.Background> 
       <SolidColorBrush x:Name="backgroundBrush" Color="Transparent" Opacity="0.1"/> 
      </Grid.Background> 
     </Grid> 
     <DataTemplate.Triggers> 
      <DataTrigger Binding="{Binding IsExpanded}" Value="True"> 
       <Setter TargetName="backgroundBrush" Property="Color" Value="Green" /> 
      </DataTrigger> 
      <Trigger SourceName="grid" Property="IsMouseOver" Value="True"> 
       <Trigger.EnterActions> 
        <BeginStoryboard> 
         <Storyboard> 
          <ColorAnimation Storyboard.TargetName="backgroundBrush" 
           Storyboard.TargetProperty="Color" 
           To="White" Duration="0:0:1.5"/> 
         </Storyboard> 
        </BeginStoryboard> 
       </Trigger.EnterActions> 
       <Trigger.ExitActions> 
        <BeginStoryboard> 
         <Storyboard> 
          <ColorAnimation Storyboard.TargetName="backgroundBrush" 
           Storyboard.TargetProperty="Color" 
           AccelerationRatio="1" Duration="0:0:1.5" /> 
         </Storyboard> 
        </BeginStoryboard> 
       </Trigger.ExitActions> 
      </Trigger> 
     </DataTemplate.Triggers> 
    </DataTemplate> 
</ListBox.ItemTemplate> 

不能编译时出现错误'无法找到触发目标'backgroundBrush'。'WPF DataTrigger找不到触发目标

如果我删除DataTrigger它将编译和工作。如果我更改DataTrigger使用TargetName="grid" Property="Background"它将编译和工作(但没有所需的不透明度)。

我哪里错了?

+0

+1,好问题。我认为我理解WPF,但我不知道为什么这不起作用...... – Heinzi 2010-03-03 21:50:56

+0

(最新评论,但尚未...)当你实际上应该有两种不同的颜色(定义作为本地资源),然后在触发器的设置者之间进行交换。我会认为这是问题的原因(尽管应该进行测试以确定)。 – heltonbiker 2014-10-20 12:26:36

回答

4

虽然我不确定为什么画笔不在名称范围内,但您可以通过替换画笔来完成此操作,并在动画中将“点击”添加到背景画笔的Color属性中,如下所示:

<ListBox.ItemTemplate> 
    <DataTemplate> 
     <Grid x:Name="grid"> 
      <Grid.Background> 
       <SolidColorBrush Color="Transparent" Opacity="0.1"/> 
      </Grid.Background> 
     </Grid> 
     <DataTemplate.Triggers> 
      <DataTrigger Binding="{Binding IsExpanded}" Value="True"> 
       <Setter TargetName="grid" Property="Background"> 
        <Setter.Value> 
         <SolidColorBrush Color="Green" Opacity="0.1"/> 
        </Setter.Value> 
       </Setter> 
      </DataTrigger> 
      <Trigger SourceName="grid" Property="IsMouseOver" Value="True"> 
       <Trigger.EnterActions> 
        <BeginStoryboard> 
         <Storyboard> 
          <ColorAnimation Storyboard.TargetName="grid" 
          Storyboard.TargetProperty="Background.Color" 
          To="White" Duration="0:0:1.5"/> 
         </Storyboard> 
        </BeginStoryboard> 
       </Trigger.EnterActions> 
       <Trigger.ExitActions> 
        <BeginStoryboard> 
         <Storyboard> 
          <ColorAnimation Storyboard.TargetName="grid" 
          Storyboard.TargetProperty="Background.Color" 
          AccelerationRatio="1" Duration="0:0:1.5" /> 
         </Storyboard> 
        </BeginStoryboard> 
       </Trigger.ExitActions> 
      </Trigger> 
     </DataTemplate.Triggers> 
    </DataTemplate> 
</ListBox.ItemTemplate> 
+0

工作 - 谢谢。 backgroundBrush实际上处于StoryBoard触发器的范围内,并且可以不加修改地工作。我只是不明白为什么它会超出DataTrigger的范围。 – djskinner 2010-03-03 22:05:08

+0

是的,我也无法弄清楚。我认为可能是故事板的TargetName在运行时被解析,而Setter的TargetName在XAML解析时被解析。 – 2010-03-04 14:39:30

+0

OP代码正在更改指定画笔的颜色属性。此代码正在改变命名元素属性的画笔。我想这就是为什么这个代码有效,而OP不是。更好的做法是将两个画笔声明为数据模板中的(不同)资源,并在设置器中引用它们。 – heltonbiker 2014-10-20 12:29:24