问题绑定到WPF中的画笔属性

时间:2010-04-29 19:33:57

标签: c# .net wpf xaml

在WPF中工作,编写自定义用户控件。当我更改类的属性值时,我试图更改Border元素的background属性。现在我正在努力将它绑定到DP,不过如果有更好的方式我愿意接受建议。

这是UserControl的XAML

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

代码......

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); }
    }
}

相反,背景是透明的。

1 个答案:

答案 0 :(得分:0)

ARGB上的'alpha'频道为0。 将alpha通道视为不透明度...将其设置为255并显示背景:)