Reputation: 697
I have created a dependency property but for some reason it seems to be failing, it must be sothing to do with the binding but I can't see what. The dependency property is as follows:
public class HighlightColour
{
#region HighlightColour dependency property
public static readonly DependencyProperty sm_ValueProperty;
public static Color GetValue(DependencyObject obj)
{
return (Color)obj.GetValue(sm_ValueProperty);
}
public static void SetValue(DependencyObject obj, Color value)
{
obj.SetValue(sm_ValueProperty, value);
}
#endregion
static HighlightColour()
{
var metadata = new FrameworkPropertyMetadata(Colors.Transparent);
sm_ValueProperty = DependencyProperty.RegisterAttached("Value",
typeof(Color),
typeof(HighlightColour), metadata);
}
}
In the XAML I am trying to bind to this in a style template and set the background colour of a button:
<Style x:Key="CommonButton" TargetType="Button">
<Setter Property="FontFamily" Value="Tahoma"/>
<Setter Property="FontSize" Value="12"/>
<Setter Property="custprop:HighlightColour.Value" Value="Red"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Grid Margin="0">
<Border Name="ButtonBody" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="1" CornerRadius="12,12,12,12" />
<Border Name="Highlight" Opacity="0" BorderThickness="0" CornerRadius="12,12,12,12">
<Border.Background>
<SolidColorBrush Color="{Binding Path=(custprop:HighlightColour.Value), Source={RelativeSource FindAncestor,AncestorType={x:Type Button}}}"/>
</Border.Background>
</Border>
<ContentPresenter Name="content" HorizontalAlignment="Center" VerticalAlignment="Center" />
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Trigger.EnterActions>
<BeginStoryboard Name="HighlightAnim">
<Storyboard TargetName="Highlight" TargetProperty="Opacity" AutoReverse="True" RepeatBehavior="Forever">
<DoubleAnimation From="1" To="0.2" Duration="00:00:01"/>
</Storyboard>
</BeginStoryboard>
</Trigger.EnterActions>
<Trigger.ExitActions>
<StopStoryboard BeginStoryboardName="HighlightAnim"/>
</Trigger.ExitActions>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
For some reason it is failing and I see errors such as:
Cannot get 'Value' value (type 'Color') from '' (type 'RelativeSource'). BindingExpression:Path=(0); DataItem='RelativeSource' (HashCode=60828848); target element is 'SolidColorBrush' (HashCode=10588721); target property is 'Color' (type 'Color') InvalidCastException:'System.InvalidCastException: Unable to cast object of type 'System.Windows.Data.RelativeSource' to type 'System.Windows.DependencyObject'.
Upvotes: 1
Views: 4063
Reputation: 16618
Change:
<SolidColorBrush Color="{Binding Path=(custprop:HighlightColour.Value),
Source={RelativeSource FindAncestor,AncestorType={x:Type Button}}}" />
To:
<SolidColorBrush Color="{Binding Path=(custprop:HighlightColour.Value),
RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type Button}}}" />
For more information on Binding: MSDN or this cheat sheet
Upvotes: 2
Reputation: 178630
Don't prefix with sm_
or WPF won't be able to "find" your DP.
Upvotes: 0