【发布时间】: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); }
}
}
相反,背景是透明的。
【问题讨论】: