【问题标题】:Cannot resolve all property references in the property path 'TextElement.Foreground'无法解析属性路径“TextElement.Foreground”中的所有属性引用
【发布时间】:2014-10-14 19:26:22
【问题描述】:

我在我的 wpf 应用程序中为按钮设置了一个控件模板,但在我的情节提要中为 ContentPresenter(即文本)前景色设置动画时遇到了问题。

下面是设置按钮的xaml

<ControlTemplate x:Key="SaveButton" TargetType="{x:Type ButtonBase}">
    <Border x:Name="buttonBorder" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="auto" />
                <ColumnDefinition Width="auto" />
            </Grid.ColumnDefinitions>
            <Control Grid.Column="0" Template="{DynamicResource save_icon}" 
                     Margin="10,0,0,0"
                     />
            <TextBlock x:Name="buttonContent" 

                              Text="Save"                                   

                              Focusable="False" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
                              Margin="{TemplateBinding Padding}" 

                              SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" 
                              VerticalAlignment="{TemplateBinding VerticalContentAlignment}"                                  
                              Grid.Column="1"/>
        </Grid>
    </Border>
    <ControlTemplate.Triggers>
        <Trigger Property="Button.IsDefaulted" Value="True">
            <Setter Property="BorderBrush" TargetName="buttonBorder" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
        </Trigger>

            <EventTrigger RoutedEvent="MouseEnter">
                <BeginStoryboard>
                    <Storyboard>
                        <ColorAnimation
                                To="#000" Duration="0:0:0.2"
                                Storyboard.TargetName="buttonBorder" 
                                Storyboard.TargetProperty="Background.Color"/>
                    <ColorAnimation
                                To="#fff" Duration="0:0:0.2"
                                Storyboard.TargetName="buttonContent" 
                                Storyboard.TargetProperty="TextElement.Foreground"/>
                </Storyboard>
                </BeginStoryboard>
            </EventTrigger>
            <EventTrigger RoutedEvent="MouseLeave">
                <BeginStoryboard>
                    <Storyboard>
                        <ColorAnimation
                                To="#fff" Duration="0:0:0.2"
                                Storyboard.TargetName="buttonBorder"
                                Storyboard.TargetProperty="Background.Color"/>
                    <ColorAnimation
                                To="#000" Duration="0:0:0.2"
                                Storyboard.TargetName="buttonContent" 
                                Storyboard.TargetProperty="TextElement.Foreground"/>
                </Storyboard>
                </BeginStoryboard>
            </EventTrigger>            


    </ControlTemplate.Triggers>
</ControlTemplate>

但是在鼠标悬停时出现以下错误 - 无法解析属性路径“TextElement.Foreground”中的所有属性引用

为什么会这样,我该如何解决?

【问题讨论】:

  • 显然,您不能为ContentPresenter 上的TextElement.Foreground Attached 属性设置动画。如果您仅将其用作文本,则只需将其替换为 TextBlock 即可。
  • 所以我用
  • 刚刚更新了我的代码以显示从 ContentPresenter 到 Textblock 的变化

标签: wpf xaml animation storyboard contentpresenter


【解决方案1】:
<ColorAnimation Storyboard.TargetProperty="(TextBlock.Background).(SolidColorBrush.Color)" To="Red"/>
<ColorAnimation Storyboard.TargetProperty="(TextBlock.Foreground).(SolidColorBrush.Color)" To="Red"/>

怀疑者的完整示例

        <Button Content="Context">
        <Button.Template>
            <ControlTemplate TargetType="Button">
                <Border Name="bord" Background="CadetBlue">
                    <TextBlock Name="tekst" Text="text" Foreground="Black"/>
                </Border>
                <ControlTemplate.Triggers>
                    <EventTrigger RoutedEvent="MouseEnter">
                        <BeginStoryboard>
                            <Storyboard>
                                <ColorAnimation Storyboard.TargetName="bord" Storyboard.TargetProperty="(Border.Background).(SolidColorBrush.Color)" To="Red"/>
                                <ColorAnimation Storyboard.TargetName="tekst" Storyboard.TargetProperty="(TextBlock.Foreground).(SolidColorBrush.Color)" To="White"/>
                            </Storyboard>
                        </BeginStoryboard>
                    </EventTrigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Button.Template>
    </Button>

【讨论】:

  • 为什么我必须这样做呢?
  • @ocajian 这就是为什么这被认为是一个糟糕的答案,根本没有解释。另外我不确定它是否适用于Background,你确定它适用于Background吗?顺便说一句,要指定DependencyProperty,您需要使用括号将其包裹起来。你也可以只使用这个(TextBlock.Foreground).Color(假设前景应该是SolidColorBrush)。
  • Background/Foreground 属性包含像 SolidColorBrush/LinearColorBrush 这样的画笔,默认是第一个。您想要为画笔的属性 Color 设置动画,这就是必须包含此路径的原因。
  • 我们只有所谓的LinearGradientBrush,而不是LinearColorBrush,这也只适用于SolidColorBrush。如果原来的ForegroundLinearGradientBrush,会有一些错误在运行时。
  • @ocajian 请确认Background,我认为它不起作用。通常我们甚至不能在 XAML 代码中为ContentPresenter 设置Background
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-12-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-21
  • 1970-01-01
相关资源
最近更新 更多