I'm creating dependency property in my custom controll cla开发者_StackOverflow中文版ss 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>
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.
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.
精彩评论