wpf DynamicResource 颜色对边框上的 BorderBrush 不起作用 - 错误?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17789648/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
DynamicResource color doesn't work for BorderBrush on a Border - Bug?
提问by pfw
Visual Studio 2010 | .NET/WPF 4.0
视觉工作室 2010 | .NET/WPF 4.0
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!
我认为这可能是 WPF 错误,但我似乎找不到关于它的错误报告。为了弥补我只是遗漏了一些明显的东西的可能性,我转向 stackoverflow 寻求答案!
Consider this xaml (nothing in the codebehind):
考虑这个 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>
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.
当我运行代码时,外边框没有边框 - 看起来它只是没有加载。背景正确设置为红色。同时,内边框确实正确加载了它的蓝色边框。
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.\
如果我将所有“DynamicResource”更改为“StaticResource”,它会在运行时正确呈现。不一致真的困扰着我,我想不通。\
So:
所以:
- Why doesn't DynamicResource work for BorderBrush?
- Given #1, why doesit work for Background?
- Why does explicitly defining the solid color brush in the resources seem to fix things?
- 为什么 DynamicResource 不适用于 BorderBrush?
- 鉴于#1,为什么没有它的后台工作?
- 为什么在资源中明确定义纯色画笔似乎可以解决问题?
EDIT:
编辑:
Looks like it's a bug that MS decided not to fix (thanks to Sheridanfor the link): http://connect.microsoft.com/VisualStudio/feedback/details/589898/wpf-border-borderbrush-does-not-see-changes-in-dynamic-resource
看起来这是 MS 决定不修复的错误(感谢Sheridan提供链接):http: //connect.microsoft.com/VisualStudio/feedback/details/589898/wpf-border-borderbrush-does-not-see-动态资源变化
采纳答案by Sheridan
Apparently, the answer to your question is no, this behaviour is nota bug.
显然,您的问题的答案是否定的,这种行为不是错误。
This issue was posted on the Microsoft Connect site by a user and the following reply was given:
此问题由用户在 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?"
DynamicResources 在运行时而不是编译时“查找”。“动态”不是指“可以随时动态更新”,而是“我们稍后会在实际需要时查找”。
如果您想在运行时更改边框画笔,您需要将 Name="" 属性应用于 Border 以便从代码隐藏中触摸它,或者您可以使用 Binding 将画笔的值设置为DependencyProperty(如果您使用的是 MVVM 模式或类似的东西)。更改属性,绑定系统会更新边框画笔。
顺便说一句,这在 StackOverflow 上会是一个很好的问题——“为什么我的 DynamicResource 没有更新?”
Personally, I like the last line best. Microsoft at its most useful! The page can be found here.
就个人而言,我最喜欢最后一行。微软最有用!该页面可以在这里找到。
回答by Andy
This doesn't seem to be the case with the RadialGradientBrush.
RadialGradientBrush 似乎不是这种情况。
<Window x:Class="WpfApplication3.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.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}"/>
</Grid.Resources>
<Border BorderThickness="20">
<Border.BorderBrush>
<RadialGradientBrush>
<GradientStop Color="{DynamicResource BorderColor}"/>
<GradientStop Color="{DynamicResource BorderColor}"/>
</RadialGradientBrush>
</Border.BorderBrush>
<Border.Background>
<SolidColorBrush Color="{DynamicResource BackgroundColor}"/>
</Border.Background>
</Border>
<Border Margin="40" BorderBrush="{DynamicResource BorderColorBrush}" BorderThickness="20"/>
</Grid>
</Window>


回答by Nitesh
Another interesting thing is that it does not happen when use a Rectangle instead of a Border.
另一个有趣的事情是使用 Rectangle 而不是 Border 时不会发生这种情况。
<Rectangle StrokeThickness="20">
<Rectangle.Stroke>
<SolidColorBrush Color="{DynamicResource BorderColor}"/>
</Rectangle.Stroke>
<Rectangle.Fill>
<SolidColorBrush Color="{DynamicResource BackgroundColor}"/>
</Rectangle.Fill>
</Rectangle>
回答by Sergey Belikov
It seems to be fixed in 4.5. In my case it works in Windows 8, but don't work in Windows XP (that don't have .net 4.5).
它似乎在 4.5 中已修复。就我而言,它适用于 Windows 8,但不适用于 Windows XP(没有 .net 4.5)。
回答by Muhammad Rehan Saeed
Here is a custom control which you can use in place of the Border. It fixes the problem with the BorderBrush property. It uses Rectangles which work as another answer indicates. Please note that this control will probably not match the performance of using the Border control but it does work, so I suggest only using it where necessary.
这是一个自定义控件,您可以使用它来代替边框。它修复了 BorderBrush 属性的问题。它使用作为另一个答案指示的矩形。请注意,此控件可能与使用 Border 控件的性能不匹配,但它确实有效,因此我建议仅在必要时使用它。
<Style TargetType="{x:Type controls:BorderFix}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type controls:BorderFix}">
<DockPanel x:Name="PART_Container"
Background="{TemplateBinding Background}"
LastChildFill="True"
UseLayoutRounding="{TemplateBinding UseLayoutRounding}">
<Rectangle x:Name="PART_LeftBorder"
DockPanel.Dock="Left"
Fill="{TemplateBinding BorderBrush}"
Width="{Binding Path=BorderThickness.Left, RelativeSource={RelativeSource TemplatedParent}}"/>
<Rectangle x:Name="PART_TopBorder"
DockPanel.Dock="Top"
Fill="{TemplateBinding BorderBrush}"
Height="{Binding Path=BorderThickness.Top, RelativeSource={RelativeSource TemplatedParent}}"/>
<Rectangle x:Name="PART_RightBorder"
DockPanel.Dock="Right"
Fill="{TemplateBinding BorderBrush}"
Width="{Binding Path=BorderThickness.Right, RelativeSource={RelativeSource TemplatedParent}}"/>
<Rectangle x:Name="PART_BottomBorder"
DockPanel.Dock="Bottom"
Fill="{TemplateBinding BorderBrush}"
Height="{Binding Path=BorderThickness.Bottom, RelativeSource={RelativeSource TemplatedParent}}"/>
<ContentPresenter x:Name="PART_Content"/>
</DockPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
public sealed class BorderFix : ContentControl
{
static BorderFix()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(BorderFix), new FrameworkPropertyMetadata(typeof(BorderFix)));
}
}
The fact that we have to do this is pretty ridiculous. Another answer suggests that this bug is fixed in the version of .NET used by Windows 8. I have not tested that but let us hope that is correct. .NET 4.5.51209 exhibits the same problem.
我们必须这样做的事实非常荒谬。另一个答案表明此错误已在 Windows 8 使用的 .NET 版本中得到修复。我尚未测试过,但我们希望这是正确的。.NET 4.5.51209 也存在同样的问题。

