Problem Binding to a Brush Property in WPF

Posted by Krisc on Stack Overflow See other posts from Stack Overflow or by Krisc
Published on 2010-04-29T19:33:57Z Indexed on 2010/04/29 19:37 UTC
Read the original article Hit count: 189

Filed under:
|
|
|

Working in WPF, writing a custom user control. I am trying to change the background property of the Border element when I change the value of a property of the class. Right now I am working on simply binding it to a DP, though if there is a better way I am open to suggestions.

Here is the XAML for the UserControl

<UserControl x:Class="MyProject.MyControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:js="clr-namespace:MyProject"
             mc:Ignorable="d" 
             x:Name="MyControlRootLayout"
             Background="Transparent"
             d:DesignHeight="300" d:DesignWidth="300" Cursor="Hand">
    <Border x:Name="RootBorder" 
            Background="{Binding Path=CoreBackground, ElementName=MyControlRootLayout}"
    >
    </Border>
</UserControl>

And the code...

public partial class MyControl : UserControl
{
    public static DependencyProperty IsSelectedProperty = DependencyProperty.Register("IsSelected", typeof(bool), typeof(MyControl));
    public static DependencyProperty CoreBackgroundProperty = DependencyProperty.Register("CoreBackground", typeof(Brush), typeof(MyControl));

    public MyControl()
    {
        CoreBackground = new SolidColorBrush(Color.FromArgb(0, 255, 245, 104));

        InitializeComponent();

        Margin = new Thickness(5);
    }

    public Brush CoreBackground
    {
        get { return (Brush)GetValue(CoreBackgroundProperty); }
        set { SetValue(CoreBackgroundProperty, value); }
    }

    public bool IsSelected
    {
        get { return (bool)GetValue(IsSelectedProperty); }
        private set { SetValue(IsSelectedProperty, value); }
    }
}

Instead, the background comes out as transparent.

© Stack Overflow or respective owner

Related posts about wpf

Related posts about xaml