0
在WPF中工作,編寫自定義用戶控件。我試圖在更改該類的屬性值時更改Border元素的背景屬性。現在我正在將它綁定到DP上,但如果有更好的方法,我願意接受建議。在WPF中綁定到Brush屬性的問題
下面是用戶控件
<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>
和代碼的XAML ...
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); }
}
}
相反,背景出來的透明。