C# 无法在样式中更改自己的属性

C# 无法在样式中更改自己的属性,c#,wpf,styles,dependency-properties,C#,Wpf,Styles,Dependency Properties,我正在自定义控件类数据点中创建依赖项属性: public abstract class DataPoint : Control { public Color BaseColor { get { return (Color)GetValue(BaseColorProperty); } set { SetValue(BaseColorProperty, value); } } publi

我正在自定义控件类数据点中创建依赖项属性:

public abstract class DataPoint : Control
{
        public Color BaseColor
        {
            get { return (Color)GetValue(BaseColorProperty); }
            set { SetValue(BaseColorProperty, value); }
        }

        public static readonly DependencyProperty BaseColorProperty =
            DependencyProperty.Register("BaseColor", typeof(Color), typeof(DataPoint), new UIPropertyMetadata(Colors.DarkRed));

    // Other class stuff
}
public class AreaDataPoint : DataPoint
{
        static AreaDataPoint()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(AreaDataPoint), new FrameworkPropertyMetadata(typeof(AreaDataPoint)));
        }

    // Other class stuff
}
然后创建其他自定义控件区域DataPoint继承DataPoint:

public abstract class DataPoint : Control
{
        public Color BaseColor
        {
            get { return (Color)GetValue(BaseColorProperty); }
            set { SetValue(BaseColorProperty, value); }
        }

        public static readonly DependencyProperty BaseColorProperty =
            DependencyProperty.Register("BaseColor", typeof(Color), typeof(DataPoint), new UIPropertyMetadata(Colors.DarkRed));

    // Other class stuff
}
public class AreaDataPoint : DataPoint
{
        static AreaDataPoint()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(AreaDataPoint), new FrameworkPropertyMetadata(typeof(AreaDataPoint)));
        }

    // Other class stuff
}
在xaml中,我试图为BaseColor属性赋值,但它不起作用

<Style TargetType="{x:Type local1:AreaDataPoint}">
    <Setter Property="BaseColor" Value="DarkGreen" />
</Style>

您正在设置背景属性,而不是BaseColor属性

<Style TargetType="{x:Type local1:AreaDataPoint}">
    <Setter Property="BaseColor" Value="DarkGreen" />
</Style>

请记住,您可能需要一个转换器来获取字符串暗绿色并将其转换为颜色指示。

您如何确定它不起作用。从这里的代码来看,一切都是正确的,因此您可能在其他地方做了一些导致样式值丢失的事情。您是否可能在代码或XAML中的某个地方为BaseColor设置本地值?您是否验证了使用BaseColor属性的位置是否正确提取了值?直接使用颜色也不常见——大多数情况下,前景、背景、形状填充都使用画笔,所以我也要检查一下。

我在xaml中错了。属性名为BaseColor。但它也不起作用。正如我所说,你可能需要一个转换器来获取暗绿色的值并将其转换为颜色。测试它的一种方法是将DependencyProperty的类型更改为字符串,并查看是否调用setter。XAML中的命名颜色和SolidColorBrush会自动转换。什么意思是它不起作用。您能告诉我们错误消息、异常或错误结果等吗?