Binding DependencyProperty to a usercontrol?

Up vote 1 down vote favorite share g+ share fb share tw.

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: 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'. C# wpf xaml binding dependency-properties link|improve this question edited Jan 16 at 13:22Didier Ghys11.2k41124 asked Jan 12 at 13:08user1145533436 62% accept rate.

1 Please show us the XAML – Baboon Jan 12 at 13:09 Have just added the XAML – user1145533 Jan 12 at 13:13.

Change: To: For more information on Binding: MSDN or this cheat sheet.

Thanks Baboon unfortunately that didn't work, although the breakpoint in the constructor of HighlightColour now gets hit. – user1145533 Jan 12 at 13:26 @user1145533 please see my edited answer – Baboon Jan 12 at 13:29 In the XAML I was trying to create a SolidcolourBrush using the colour in dependencyProperty so shouldn't the DependencyProperty be declared as a Color? The Error I gt now is: System.Windows.

Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls. Button', AncestorLevel='1''. BindingExpression:Path=(0); DataItem=null; target element is 'SolidColorBrush' (HashCode=32830290); target property is 'Color' (type 'Color') – user1145533 Jan 12 at 13:44 No you're right, I misread the XAML.

It should be a Color indeed. Try removing the ". Value" from the Binding – Baboon Jan 12 at 13:50 With that I get a path error:System.Windows.

Data Error: 40 : BindingExpression path error: '(custprop:HighlightColour)' property not found on 'object' ''Button' (Name='')'. BindingExpression:Path=(custprop:HighlightColour); DataItem='Button' (Name=''); target element is 'SolidColorBrush' (HashCode=13202999); target property is 'Color' (type 'Color') Do I have to set the DataContext to get this binding to work? I am not really sure what I would set it to.

– user1145533 Jan 12 at 13:54.

Don't prefix with sm_ or WPF won't be able to "find" your DP.

Thanks for your suggestion I just tried that but it didn't work. – user1145533 Jan 12 at 13:19 I'm pretty sure only the suffix matters. – Baboon Jan 12 at 13:22.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions