Rakuen42
Rakuen42

Reputation: 1437

Border With Uniform BorderThickness Fails to Bind BorderBrush Color

Minimal, Complete, Verifiable Example (.NET Framework 4.0+):

MainWindowViewModel.cs

namespace MCVBorderTest
{
    public class MainWindowViewModel
    {
        public string BorderColor { get { return "Red"; } }
    }
}

MainWindow.xaml

<Window x:Class="MCVBorderTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Border BorderThickness="1">
        <Border.BorderBrush>
            <SolidColorBrush Color="{Binding BorderColor}" />
        </Border.BorderBrush>
    </Border>
</Window>

MainWindow.xaml.cs

using System.Windows;

namespace MCVBorderTest
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    }
}

App.xaml

<Application x:Class="MCVBorderTest.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:local="clr-namespace:MCVBorderTest">
</Application>

App.xaml.cs

using System.Windows;

namespace MCVBorderTest
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        protected override void OnStartup(StartupEventArgs e)
        {
            new MainWindow() { DataContext = new MainWindowViewModel() }.Show();
        }
    }
}

App.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
    </startup>
</configuration>

Problem:

Running the application will result in a window opening but the border has no color. The Debug output has this message:

System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=BorderColor; DataItem=null; target element is 'SolidColorBrush' (HashCode=8990007); target property is 'Color' (type 'Color')

Changing the BorderThickness to a non-uniform value, say 0,1,1,1, will result in the border receiving its expected color and no binding error in the Debug output.

Question:

Why does the BorderBrush's Color binding behave this way?

Upvotes: 2

Views: 209

Answers (1)

Mark Feldman
Mark Feldman

Reputation: 16119

This looks like a genuine bug to me, note the different behavior between the border brush and background:

<Border BorderThickness="10">
    <Border.BorderBrush>
        <SolidColorBrush Color="{Binding BorderColor}" />
    </Border.BorderBrush>
    <Border.Background>
        <SolidColorBrush Color="{Binding BorderColor}" />
    </Border.Background>
</Border>

An apparent workaround is to give your window a x:Name ("_this") and bind via the DataContext explicitly:

<SolidColorBrush Color="{Binding ElementName=_this, Path=DataContext.BorderColor}" />

Sadly, binding via RelativeSource also seems to exhibit this issue.

Upvotes: 1

Related Questions