views:

27

answers:

2

I'm creating dependency property in my custom controll class 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
}

Then I create other custom control AreaDataPoint inheriting DataPoint:

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

    // Other class stuff
}

In xaml I'm trying to assign value to BaseColor property, but it doesn't work

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

You're setting the "Background" property, not the "BaseColor" property?

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

Bear in mind you might need a converter to take the string DarkGreen and convert it to a Color instsance.

OJ
I was wrong in xaml. Property name is BaseColor. But it also does not work.
rotor
As I said, you may need a converter to take the value of "DarkGreen" and convert it to a color. One way to test it is to change the type of your DependencyProperty to a string and see if the setter gets called.
OJ
Named Colors and SolidColorBrushes in XAML are automatically converted.
John Bowen
A: 

How are you determining that it "doesn't work". From what's in your code here everything is correct so you may be doing something elsewhere that's causing the Style value to get lost. Are you possibly setting a local value for the BaseColor somewhere in code or XAML? Have you verified that where you're using the BaseColor property is actually pulling the value correctly? It's also not common to use a Color directly - most situations (Foregrounds, Backgrounds, Shape Fills) use Brushes so I'd check that too.

John Bowen