c# - 使用触发器设置 BorderBrush 的颜色

标签 c# .net wpf xaml triggers

我在ControlTemplate 中正确使用语法时遇到了一些困难。这是它的基础知识:

<ControlTemplate TargetType="{x:Type foo:bar">
    <Border Name="Bd">  
        <Border.BorderBrush>
            <SolidColorBrush Color="{DynamicResource DefaultBorderBrushLightBrush}" />
        </Border.BorderBrush>
    </Border>

    <ControlTemplate.Triggers>
        <Trigger Property="IsSelected" Value="true">
            <Setter Property="(Border.BorderBrush).(SolidColorBrush.Color)"
                    TargetName="Bd"
                    Value="{DynamicResource PressedBorderDarkColor}"/>
        </Trigger>
    </ControlTemplate.Triggers>
</ControlTemplate

这会产生错误信息

Cannot resolve the Template Property 'Color)'. Verify that the owning type is the Style's TargetType, or use Class.Property syntax to specify the property.

这似乎很清楚 - 我没有正确指定目标属性。我已经尝试了几种不同的方法。具体来说,我试过了

<Setter Property="BorderBrush" TargetName="Bd">
    <Setter.Value>
        <SolidColorBrush Color="{DynamicResource PressedBorderDarkColor}" />
    </Setter.Value>
</Setter>

... 这确实构建了,我想,它给了我我一直在寻找的东西。

为什么我不能设置画笔颜色?我将如何指定它?

更广泛地说,我在哪里可以了解有关此“Class.Property 语法”的更多信息?我通读了 MSDN 的 XAML Syntax In Detail但如果它被覆盖在那里,我担心我忽略了它。

最佳答案

不能仅仅因为它不是目标边框对象 Bd 的属性而设置画笔颜色,目标对象具有属性 BorderBrush,但是颜色是受 BorderBrush 属性影响的对象 SolidColorBrush 的属性。语法 (Border.BorderBrush).(SolidColorBrush.Color) 用于例如当您必须在动画中指定 StoryBoard 的附加属性 TargetProperty 时:

<VisualState x:Name="MouseOver">
                <Storyboard>
                  <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).
                      (GradientBrush.GradientStops)[1].(GradientStop.Color)"
                                                Storyboard.TargetName="Border">
                    <EasingColorKeyFrame KeyTime="0"
                                         Value="{StaticResource ControlMouseOverColor}" />
                  </ColorAnimationUsingKeyFrames>
                </Storyboard>
              </VisualState>

关于c# - 使用触发器设置 BorderBrush 的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21938613/

相关文章:

.net - .net 4.0 动态变量的内存是如何分配的?

wpf - 在 WPF 中制作日志窗口的建议

wpf - 直接在 WPF MVVM 中查看更新模型,而不是 ViewModel

c# - 如何在 Visual Studio 2010 中恢复损坏的 .csproj 文件?

javascript - 如何在 Windows Phone 8.1 应用程序中在 C# 和 Javascript 之间传递数据或通信

c# - 如何获取任务管理器中显示的应用程序内存使用情况?

c# - 在新机器上安装后抛出 InvalidCastException

c# - wpf 窗口刷新首先起作用,然后停止

c# - 对于 C# 中的匹配,Regex 实例线程是否安全

c# - 带有 c# 后端的 Angular 2 Http 获取变量不起作用