I created a base ClassAthat contains DependencyProperty MyText. I received from him ClassB. This one ClassBis built in MyCustomButton. In MouseHover / MousePressed, I change the MyText value of the built-in ClassB class. But, while the parent ClassA fires the PropertyChanged-Event, the derived class B never changes the Property.
What would be the right way to notify the derived class B of a changing DependencyProperty? I already tried the solution mentioned in changing the DependencyProperty value of the base class in WPF ", but could not get this to work.
My code is:
The main window consists of only one MyCustomButton containing the ClassB-Instance:
<Window.Resources>
<cc:ClassB x:Key="MyClassB" MyText="MyClassBText"/>
</Window.Resources>
<Grid>
<StackPanel>
<cc:MyCustomButton MyClassA="{StaticResource MyClassB}" Width="100" Height="100"/>
</StackPanel>
</Grid>
MyCustomButton ClassA (, B):
class MyCustomButton : Button
{
public ClassA MyClassA
{
get { return (ClassA)GetValue(MyClassAProperty); }
set { SetValue(MyClassAProperty, value); }
}
public static readonly DependencyProperty MyClassAProperty =
DependencyProperty.Register("MyClassA", typeof(ClassA), typeof(MyCustomButton));
}
<Style TargetType="{x:Type cc:MyCustomButton}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type cc:MyCustomButton}">
<Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}">
<StackPanel Background="Transparent">
<cc:ClassA x:Name="ButtonClassA" Content="{Binding MyClassA, RelativeSource={RelativeSource TemplatedParent}}" />
</StackPanel>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Setter TargetName="ButtonClassA" Property="MyText" Value="HOVER"/>
</Trigger>
<Trigger Property="IsPressed" Value="true">
<Setter TargetName="ButtonClassA" Property="MyText" Value="PRESSED"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
ClassA DependencyProperty:
public class ClassA : UserControl
{
public string MyText
{
get { return (string)GetValue(MyTextProperty); }
set { SetValue(MyTextProperty, value); }
}
public static readonly DependencyProperty MyTextProperty =
DependencyProperty.Register("MyText", typeof(string), typeof(ClassA), new PropertyMetadata("ClassA Default Text", MyTextPropertyChanged));
private static void MyTextPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
Console.WriteLine("ClassA.MyTextPropertyChanged: " + e.NewValue);
}
public ClassA()
{
}
}
ClassB ClassA
static ClassB()
{
MyTextProperty.AddOwner(typeof(ClassB), new PropertyMetadata(MyTextPropertyChanged));
}
private static void MyTextPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
Console.WriteLine("ClassB.MyTextPropertyChanged: " + e.NewValue);
}
public ClassB()
{
InitializeComponent();
}
<local:ClassA
xmlns:local="clr-namespace:CustomControlPlayground.CustomControls"
x:Class="CustomControlPlayground.CustomControls.ClassB"
x:Name="ClassBXAML">
<Grid>
<TextBlock Text="{Binding MyText, ElementName=ClassBXAML}"/>
</Grid>
</local:ClassA>
Update:
, , , . , , . , ClassB MyClassB ClassA?
MainWindow.xaml:
<Window.Resources>
<cc:ClassB x:Key="MyClassB" MyText="MyClassBText"/>
</Window.Resources>
<Grid>
<cc:MyCustomButton MyClassA="{StaticResource MyClassB}" Width="100" Height="100"/>
</Grid>
MyButton-Style:
<StackPanel Background="Transparent">
<cc:ClassA x:Name="ButtonClassA" Content="{Binding MyClassA, RelativeSource={RelativeSource TemplatedParent}}" />
</StackPanel>