In background property for instance we can do like this
<cs:CustomControl.Background>
<SolidColorBrush Color="{Binding BackGround}"/>
</cs:CustomControl.Background>
SolidColorBrush
is a separate class and contains separate dependency properties, and it working fine and the background property is updating as expected.
I'm trying to do the same concept on my CustomControl
.
I have create a subclass with dependency property like this
public class SubClass : DependencyObject
{
public double SubProperty1
{
get { return (double)GetValue(SubProperty1Property); }
set { SetValue(SubProperty1Property, value); }
}
// Using a DependencyProperty as the backing store for SubProperty1. This enables animation, styling, binding, etc...
public static readonly DependencyProperty SubProperty1Property =
DependencyProperty.Register("SubProperty1", typeof(double), typeof(SubClass), new PropertyMetadata(10.0d, OnSubProperty1Changed));
private static void OnSubProperty1Changed(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
//never called;
}
public string SubProperty2
{
get { return (string)GetValue(SubProperty2Property); }
set { SetValue(SubProperty2Property, value); }
}
// Using a DependencyProperty as the backing store for SubProperty1. This enables animation, styling, binding, etc...
public static readonly DependencyProperty SubProperty2Property =
DependencyProperty.Register("SubProperty2", typeof(string), typeof(SubClass), new PropertyMetadata("test", OnSubProperty2Changed));
private static void OnSubProperty2Changed(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
//never called;
}
}
And in my main class for my custom tool, I created dependency property as follows:
public SubClass MainProperty
{
get { return (SubClass)GetValue(MainPropertyProperty); }
set { SetValue(MainPropertyProperty, value); }
}
// Using a DependencyProperty as the backing store for MainProperty. This enables animation, styling, binding, etc...
public static readonly DependencyProperty MainPropertyProperty =
DependencyProperty.Register("MainProperty", typeof(SubClass), typeof(CustomControl), new PropertyMetadata((SubClass)null, OnMainPropertyChanged));
private static void OnMainPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
// called when object created only and not called again when the property changed on the UI
}
Then in my WPF window I did as following
<cs:CustomControl.MainProperty>
<cs:SubClass SubProperty1="{Binding Length}" SubProperty2="{Binding Title}"/>
</cs:CustomControl.MainProperty>
my code compiles but dependecy properties not updating and callback function not called when the viewmodel update
how can I make this work? and what do I miss I hope there is any idea with detailed code thanks in advance
CodePudding user response:
In the Output Window in Visual Studio you should have seen something like
System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. ...
when you debug your application. This is because your control and its DependencyObject property do not form a logical tree.
In order to build a logical tree, SubClass must derive from Freezable
public class SubClass : Freezable
{
...
protected override Freezable CreateInstanceCore()
{
return new SubClass();
}
}
and CustomControl must add SubClass instances to its logical children collection:
private static void OnMainPropertyChanged(
DependencyObject o, DependencyPropertyChangedEventArgs e)
{
if (e.OldValue != null)
{
((CustomControl)o).RemoveLogicalChild(e.OldValue);
}
if (e.NewValue != null)
{
((CustomControl)o).AddLogicalChild(e.NewValue);
}
}