<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",它将编译并正常工作(但没有所需的不透明度)。

我要去哪里错了?

最佳答案

虽然我不确定为什么笔刷不在namescope中,但是您可以通过换出笔刷,然后像这样在动画中“点按”到背景笔刷的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>

关于wpf - WPF DataTrigger找不到触发器目标,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/2375123/

10-11 04:07