C# 如何让自定义UserControl设置嵌套椭圆的颜色?

C# 如何让自定义UserControl设置嵌套椭圆的颜色?,c#,wpf,xaml,user-controls,C#,Wpf,Xaml,User Controls,我一直在浏览各种链接,我不知道我是否需要DependencyProperty、INotifyPropertyChanged、某种绑定或其他东西 我正在开发我的第一个UserControl,以便重用。我有一个UserControl,它包含一个标签和一个彩色椭圆。我希望能够在设计时在WindowsXAML中设置椭圆颜色。我在Visual Studio 2013社区中有以下代码: <UserControl x:Class="DMS2.LegendLabel" xmlns="ht

我一直在浏览各种链接,我不知道我是否需要DependencyProperty、INotifyPropertyChanged、某种绑定或其他东西

我正在开发我的第一个UserControl,以便重用。我有一个UserControl,它包含一个标签和一个彩色椭圆。我希望能够在设计时在WindowsXAML中设置椭圆颜色。我在Visual Studio 2013社区中有以下代码:

<UserControl x:Class="DMS2.LegendLabel"
         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" 
         mc:Ignorable="d">
<StackPanel Orientation="Horizontal">
    <Ellipse Name="Indicator" Height="10" Width="10" Margin="5" Fill="Aqua"/>
    <Label> value </Label>
</StackPanel>

价值

命名空间DMS2 { 公共部分类LegendLabel:UserControl { 公共LegendLabel() { 初始化组件(); } 私人画笔椭圆_color=画笔.蔚蓝; 公共画笔传奇色彩 { 获取{返回椭圆颜色;} 设置{ellipse\u color=value;Indicator.Fill=ellipse\u color;} } } } 刷新列表 显示来自所有用户的报告 悬而未决的 观看 忙碌的 多恩 错误 关闭 关 使用如下所示的LegendColor现在已生效。我如何做以下工作

<custom_controls:LegendLabel LegendColor="Red">Pending</custom_controls:LegendLabel>
挂起

是的,您通常会在此处使用
DependencyProperty
(代码片段:
propdp

一旦有了它,您将在
PropertyChanged
处理程序中执行设置(元数据的第二个参数):


我不明白第二个“本地”来自哪里?谢谢。我猜PropertyMetadata构造函数中的0d也不正确。我更改了笔刷.Aqua以便它可以编译。它现在可以编译了,但是我仍然没有得到我想要的结果。椭圆没有显示任何颜色。@Hugh Yep,这也是我的一个错误(我是从
double
DP复制粘贴的)。通常我会初始化为
null
,但标准画笔是合理的。您能否验证
更改
事件是否已处理?您可能还需要在加载的
事件中获取“初始”状态。
<custom_controls:LegendLabel LegendColor="Red">Pending</custom_controls:LegendLabel>
    public Brush LegendColor
    {
        get { return (Brush)GetValue(LegendColorProperty); }
        set { SetValue(LegendColorProperty, value); }
    }

    // Using a DependencyProperty as the backing store for Value.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty LegendColorProperty =
        DependencyProperty.Register("LegendColor", typeof(Brush), typeof(LegendLabel), new PropertyMetadata(null, HandleBrushChange));
private static void HandleBrushChange(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
   LegendLabel local = (LegendLabel)d;

   local.Indicator.Fill = e.NewValue as Brush;
}