WPF数据触发器无法找到触发目标。

7
<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'。”

如果我删除DataTrigger,它将编译并正常工作。如果我将DataTrigger更改为使用TargetName =“grid” Property =“Background”,则它将编译并正常工作(但不具有所需的不透明度)。

我做错了什么?


+1,好问题。我以为我理解了WPF,但我不知道为什么这个不起作用... - Heinzi
(虽然晚了点,但是...)你更改了一个颜色的值,而实际上应该有两种不同的颜色(定义为本地资源),然后在触发器的设置器中进行交换。我认为这是问题的原因(尽管需要测试才能确定)。 - heltonbiker
1个回答

6

虽然我不确定为什么画刷不在命名范围内,但您可以通过更换画刷,并在动画中“点下”背景画刷的颜色属性来实现此目的:

<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>

2
可以了 - 谢谢。backgroundBrush实际上在StoryBoard触发器的范围内,并且可以正常工作而无需修改。我只是不明白为什么它会超出DataTrigger的范围。 - djskinner
是的,我也无法弄清楚。我认为可能是Storyboard的TargetName在运行时解析,而Setter的TargetName在XAML解析时解析。 - Abe Heidebrecht
OP代码正在更改命名画刷的颜色属性。 此代码正在更改命名元素属性的画刷。 我猜这就是为什么这个代码有效,而OP的代码不起作用。 更好的做法是在数据模板中声明两个画刷作为(不同的)资源,并在setter中引用它们。 - heltonbiker

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接