【问题标题】:EventTrigger on a TextBlock that sets a Border - Cannot resolve all property references设置边框的 TextBlock 上的 EventTrigger - 无法解析所有属性引用
【发布时间】:2011-10-23 22:12:01
【问题描述】:

我在 WPF 中有一个自定义控件,我在其中定义了一个大型 ItemsControl 模板。 在那里,我有一个网格,在该网格的一列中,我有一个 TextBlock,在另一列中我有一个边框。

我想在鼠标进入 TextBlock 时突出显示边框。

我尝试了几种情况: 首先是 TextBlock 样式中的 EventTrigger,但我了解到您不能这样做,然后是 TextBlock 的 Triggers 部分中的 EventTrigger,现在我只是将它放在 ItemsControl 的 DataTemplate.Triggers 中,但我一直得到错误:

"Cannot resolve all property references in the property path 'Border.BorderBrush.Color'. Verify that applicable objects support the properties."  

这是导致问题的代码:

<DataTemplate.Triggers>
    <EventTrigger SourceName="mytxtblock" RoutedEvent="TextBlock.MouseEnter">
        <EventTrigger.Actions>
            <BeginStoryboard>
                <Storyboard>
                    <ColorAnimation Storyboard.TargetName="myborder"
                                                Storyboard.TargetProperty="Border.BorderBrush.Color"
                                                Duration="0:0:1"                                                                        
                                                To="White" />
                    <ThicknessAnimation Storyboard.TargetProperty="Border.BorderThickness"
                                                    Duration="0:0:1"
                                                    From="0"
                                                    To="1" />
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger.Actions>
    </EventTrigger>
</DataTemplate.Triggers>

我认为我在引用边框的 Color 属性的方式上遗漏了一些东西,有什么见解吗?

谢谢!

编辑:我发现在Resources 中声明SolidColorBrush 然后使用该值可以让我摆脱

Storyboard.TargetProperty="Border.BorderBrush.Color" 更改为 Storyboard.TargetProperty="Border.BorderBrush"

但现在编译器告诉我,我声明的颜色(我尝试过绿色和透明)不是“To”的有效值...

【问题讨论】:

    标签: wpf path properties


    【解决方案1】:

    试试

    <ColorAnimation
        Storyboard.TargetName="myborder"
        Storyboard.TargetProperty="BorderBrush.(SolidColorBrush.Color)"
        Duration="0:0:1"
        To="White" />
    

    但你必须声明一个BorderBrush

    BorderBrush="whatever"
    

    <Border.BorderBrush>
        <SolidColorBrush Color="whatever" />
    </Border.BorderBrush>
    

    也在你的“myborder”中。

    【讨论】:

    • 我得到一个异常:'BorderBrush' 属性没有指向路径 'BorderBrush.(0)' 中的 DependencyObject。
    • 我可能永远无法掌握 WPF 绑定路径的窍门,但“BorderBrush.(SolidColorBrush.Color)”适用于我的 UserControl 的边框,非常感谢!
    • (EDIT) 这是一个 XAML PropertyPath,而不是我上面所说的绑定路径。发布这个以防它帮助其他人寻求启蒙。
    • 我没有在“BorderBrush”属性上声明/设置值的小“问题”是我所缺少的。 WPF/XAML 6 年了,还在学习!
    【解决方案2】:

    在您的ColorAnimation 上有两个属性:

    Storyboard.TargetName="myborder"
    Storyboard.TargetProperty="Border.BorderBrush.Color"
    

    这意味着myborder 有一个名为Border 的属性。我认为这会导致您的错误。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-10-14
      • 1970-01-01
      • 1970-01-01
      • 2010-12-24
      • 1970-01-01
      • 2017-08-17
      • 2016-08-19
      • 2021-05-14
      相关资源
      最近更新 更多