【问题标题】:Problem Binding to a Brush Property in WPF在 WPF 中绑定到画笔属性的问题
【发布时间】:2010-04-30 03:46:14
【问题描述】:

在 WPF 中工作,编写自定义用户控件。当我更改类的属性值时,我试图更改 Border 元素的背景属性。现在我正在努力将它简单地绑定到一个 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); }
    }
}

相反,背景是透明的。

【问题讨论】:

    标签: c# .net wpf xaml


    【解决方案1】:

    ARGB 上的“alpha”通道为 0。 将 alpha 通道视为不透明度...将其设置为 255,背景将出现 :)

    【讨论】:

      猜你喜欢
      • 2010-10-05
      • 1970-01-01
      • 2011-01-15
      • 2019-02-04
      • 2013-01-20
      • 2016-04-26
      • 1970-01-01
      • 2012-06-20
      相关资源
      最近更新 更多