C # UWP Windows 10 project

Described controls based on TextBox , when initializing, Binding works, but does not respond to INotifyProperryChanged. In the template, the control writes the following:

The "Property" property is not a DependencyProperty property. To use unattached properties in markup, you must provide a target type with an available property of the "Symbol" instance. For attached properties, the "GetSymbol" and "SetSymbol" static methods must be represented in the declaring type. "

Control Code:

 public sealed class CurrencyTextBox : TextBox { public CurrencyTextBox() { DefaultStyleKey = typeof(CurrencyTextBox); } public string Symbol { get { return (string)GetValue(CurrencySymbolProperty); } set { SetValue(CurrencySymbolProperty, value); } } public static readonly DependencyProperty CurrencySymbolProperty = DependencyProperty.Register("Symbol", typeof(string), typeof(CurrencyTextBox), new PropertyMetadata("", new PropertyChangedCallback(OnSymbolChanged))); private static void OnSymbolChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { ((CurrencyTextBox)d).Symbol = (string)e.NewValue; } } 

A string from the template (immediately writes the error described above):

 <TextBlock Grid.Row="2" Grid.Column="1" FontSize="{TemplateBinding FontSize}" FontWeight="SemiBold" Text="{TemplateBinding Symbol}" Margin="3,0,0,0"/> 
  • And show the whole template, please! - VladD
  • @VladD pastebin.com/X7D5F2dr copy of the TextBox template with another field - SYL
  • You have <ControlTemplate TargetType="Button"> , change it. Should take off. - VladD
  • @VladD There <ControlTemplate TargetType="Controls:CurrencyTextBox"> and the button inside <Grid.Resources> <Style x:Name="DeleteButtonStyle" TargetType="Button"> , in theory should not affect - SYL
  • Hmm, really. I'll check. - VladD

1 answer 1

You have an error in determining the dependency property.

It is necessary so:

 public string Symbol { get { return (string)GetValue(SymbolProperty); } set { SetValue(SymbolProperty, value); } } public static readonly DependencyProperty SymbolProperty = DependencyProperty.Register( "Symbol", typeof(string), typeof(CurrencyTextBox), new PropertyMetadata("", new PropertyChangedCallback(OnSymbolChanged))); 

The difference in the name of the static field, it is important.

With depednency property it is better not to rename them manually, but recreate it with the propdp snippet.


And apparently, you do not need OnSymbolChanged at all, you assign an assigned value there again.

  • The error disappeared, but the property is still not updated. Binding only works once, at initialization - SYL
  • The Binding update worked for Mode=TwoWay . It is not clear why, but I am very glad that at least it works)) - SYL
  • @SYL: But you do not have NotifyPropertyChanged ? In the code only dependency property. - VladD
  • @SYL: Added the answer - it seems that you do not need the OnSymbolChanged method. - VladD
  • Thank you so much for your help. Hike figured out what's what - SYL