Reputation: 121
I cannot seem to get a WPF ControlTemplate binding to update on a regular getter property using INotifyPropertyChanged. See the example below.
I have the following C# WPF Class:
public class ExampleControl : Control, INotifyPropertyChanged
{
public static readonly DependencyProperty NumberProperty =
DependencyProperty.Register("Number", typeof(int),
typeof(ExampleControl), new PropertyMetadata(0));
public int Number
{
get { return (int)GetValue(NumberProperty); }
set
{
SetValue(NumberProperty, value);
OnPropertyChanged(nameof(Text));
}
}
public string Text
{
get
{
return (Number * 2).ToString();
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
And am using this Style containing a ControlTemplate:
<Style TargetType="local:ExampleControl">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="local:ExampleControl">
<TextBlock Text="{Binding Text, RelativeSource={RelativeSourceAncestorType=local:ExampleControl}}" />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
It does work the first time the control is loaded (so the binding is working). But when the "Number" Property of ExampleControl gets updated, the TextBlock in the ControlTemplate does not update correspondingly. I do want to base the Text property on the value of the Number DependencyProperty. What do I need to do to make this happen?
Upvotes: 0
Views: 33