gpt4 book ai didi

wpf - DynamicResource 颜色对边框上的 BorderBrush 不起作用 - 错误?

转载 作者:行者123 更新时间:2023-12-03 22:53:29 27 4
gpt4 key购买 nike

视觉工作室 2010 | .NET/WPF 4.0

我认为这可能是 WPF 错误,但我似乎找不到关于它的错误报告。为了弥补我只是遗漏了一些明显的可能性,我转向 stackoverflow 寻求答案!

考虑这个 xaml(代码隐藏中没有任何内容):

<Window x:Class="DownExpanders.BorderTest"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="BorderTest" Height="300" Width="300">
<Window.Resources>
<Color x:Key="BackgroundColor" R="255" G="0" B="0" A="255"/>
<Color x:Key="BorderColor" R="0" G="0" B="255" A="255"/>
<SolidColorBrush x:Key="BorderColorBrush" Color="{DynamicResource BorderColor}"/>
</Window.Resources>
<Grid>
<Border BorderThickness="20">
<Border.Background>
<SolidColorBrush Color="{DynamicResource BackgroundColor}"/>
</Border.Background>
<Border.BorderBrush>
<SolidColorBrush Color="{DynamicResource BorderColor}"/>
</Border.BorderBrush>
</Border>

<Border Margin="40" BorderBrush="{DynamicResource BorderColorBrush}" BorderThickness="20"/>
</Grid>
</Window>

在设计器中,它按预期呈现。外边框有蓝色大边框和红色背景,内边框有蓝色大边框。伟大的。

当我运行代码时,外边框没有边框 - 看起来它只是没有加载。背景正确设置为红色。同时,内边框确实正确加载了它的蓝色边框。

如果我将所有“DynamicResource”更改为“StaticResource”,它会在运行时正确呈现。不一致真的困扰着我,我想不通。\

所以:
  • 为什么 DynamicResource 不适用于 BorderBrush?
  • 鉴于#1,为什么它适用于背景?
  • 为什么在资源中明确定义纯色画笔似乎可以解决问题?

  • 编辑:

    看起来这是 MS 决定不修复的错误(感谢 Sheridan 提供链接): http://connect.microsoft.com/VisualStudio/feedback/details/589898/wpf-border-borderbrush-does-not-see-changes-in-dynamic-resource

    最佳答案

    显然,您的问题的答案是否定的,这种行为不是错误。

    此问题由用户在 Microsoft Connect 站点上发布,并给出了以下答复:

    DynamicResources are "looked up" at runtime rather than compile time. The "Dynamic" refers not to "can be dynamically updated at any time" but "we'll look it up later, when it's actually needed."

    If you want to change the border brush at runtime, you'll need to apply a Name="" attribute to the Border in order to touch it from the codebehind, or you can use a Binding to set the value of the brush to a DependencyProperty (if you're using the MVVM pattern or something similar). Change the property and the border brush gets updated by the binding system.

    BTW, this would have been a good question over at StackOverflow--"Why isn't my DynamicResource being updated?"



    就个人而言,我最喜欢最后一行。微软最有用!页面可以找到 here .

    关于wpf - DynamicResource 颜色对边框上的 BorderBrush 不起作用 - 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17789648/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com