DynamicResource color doesn't work for BorderBrush on a Border - Bug?

前端 未结 5 2007
傲寒
傲寒 2020-12-30 09:42

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 s

相关标签:
5条回答
  • 2020-12-30 10:25

    Another interesting thing is that it does not happen when use a Rectangle instead of a Border.

        <Rectangle StrokeThickness="20">
            <Rectangle.Stroke>
                <SolidColorBrush Color="{DynamicResource BorderColor}"/>
            </Rectangle.Stroke>
            <Rectangle.Fill>
                <SolidColorBrush Color="{DynamicResource BackgroundColor}"/>
            </Rectangle.Fill>
        </Rectangle>
    
    0 讨论(0)
  • 2020-12-30 10:27

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

    0 讨论(0)
  • 2020-12-30 10:36

    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:

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

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

    0 讨论(0)
  • 2020-12-30 10:42

    This doesn't seem to be the case with the 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>
    

    enter image description here

    0 讨论(0)
  • 2020-12-30 10:48

    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.

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

    0 讨论(0)
提交回复
热议问题