I want to listen to DependencyProperty changes. This code works, but after each reload page, the Callback method is called several times using the CustomControl method ...
public partial class CustomControl : UserControl { public CustomControl() { InitializeComponent(); } public bool IsOpen { get { return (bool)GetValue(IsOpenProperty); } set { SetValue(IsOpenProperty, value); } } public static readonly DependencyProperty IsOpenProperty = DependencyProperty.Register("IsOpen", typeof(bool), typeof(CustomControl), new PropertyMetadata(IsOpenPropertyChangedCallback)); private static void IsOpenPropertyChangedCallback(DependencyObject sender, DependencyPropertyChangedEventArgs e) { Debug.WriteLine("Fire!"); } }
Update
ViewModel
private bool _isOpen; public bool IsOpen { get { return this._isOpen; } set { this.Set(() => this.IsOpen, ref this._isOpen, value); }
View
<local:CustomControl IsOpen="{Binding Path=IsOpen}" />
Example
project
- click "second page"
- click "true" (see output window)
- return
- click "second page"
- tap false (see output window)
c # windows-phone-8
Jakub krampl
source share