Visual Studio 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!
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.
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.\
So:
- Why doesn't DynamicResource work for BorderBrush?
- Given #1, why does it work for Background?
- Why does explicitly defining the solid color brush in the resources seem to fix things?
EDIT:
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.
This issue was posted on the Microsoft Connect site by a user and the following reply was given:
Personally, I like the last line best. Microsoft at its most useful! The page can be found here.
This doesn't seem to be the case with the RadialGradientBrush.
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).
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.
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.
Another interesting thing is that it does not happen when use a Rectangle instead of a Border.