DynamicResource颜色不能用于BorderBrush的边框 - Bug? [英] DynamicResource color doesn't work for BorderBrush on a Border - Bug?

查看:249
本文介绍了DynamicResource颜色不能用于BorderBrush的边框 - Bug?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Visual Studio 2010 | .NET / WPF 4.0

Visual Studio 2010 | .NET/WPF 4.0

我认为这可能是一个WPF错误,但我似乎找不到一个错误报告。

I think this might be a WPF bug, but I can't seem to find a bug report about it. To cover the possibility that I'm just missing something obvious, I turn to stackoverflow for answers!

考虑这个xaml(codebehind中没有):

Consider this xaml (nothing in the codebehind):

<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>

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

In the designer, it renders as expected. The outer border has a big blue border and a red background, the inner border has a big blue border. Great.

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

When I run the code, the outer border has NO border - it looks like it just doesn't load. The background is set to red correctly. Meanwhile, the inner border does load its blue border correctly.

如果我将所有DynamicResource更改为StaticResource,则在运行时会正确呈现。

If I change all "DynamicResource" to "StaticResource", it renders correctly when run. The inconsistency is really bugging me, and I can't figure it out.\

所以:


  1. 为什么DynamicResource不能用于BorderBrush?

  2. 给定#1,为什么 b $ b
  3. 为什么在资源中明确定义固体颜色画笔似乎可以解决问题?

编辑:

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

Looks like it's a bug that MS decided not to fix (thanks to Sheridan for the link): http://connect.microsoft.com/VisualStudio/feedback/details/589898/wpf-border-borderbrush-does-not-see-changes-in-dynamic-resource

推荐答案

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

Apparently, the answer to your question is no, this behaviour is not a bug.

这个问题发布在用户的Microsoft Connect网站上,并给出了以下回复:

This issue was posted on the Microsoft Connect site by a user and the following reply was given:


DynamicResources在运行时被查找,而不是编译时。
动态不是可以随时动态更新
,但我们会在以后,当它实际需要时查找。

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."

如果您想在运行时更改边框画笔,您需要
将一个Name =属性应用到边框才能触摸从
codebehind,或者你可以使用绑定将刷的值设置为
a DependencyProperty(如果你使用MVVM模式或某事
类似)。更改属性和边框画笔由
绑定系统更新。

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,这将是一个很好的问题在StackOverflow - 为什么
不是我的DynamicResource更新?

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

就我个人而言,我最喜欢最后一行。微软在其最有用!该页面可以找到此处

Personally, I like the last line best. Microsoft at its most useful! The page can be found here.

这篇关于DynamicResource颜色不能用于BorderBrush的边框 - Bug?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆