简体   繁体   中英

WPF :: Custom control how to Fire a method when a user change a property

Hello I created a custom control class that its derived from a Grid, so is there a function to override when a user change a property? for example if i have a property called Count how can i know when the user change the count property ?

Thanks alot

EDIT

i'm doing some progress

protected override void OnPropertyChanged(System.Windows.DependencyPropertyChangedEventArgs e){

}

now on my designer window the custom control doesn't change when i change the property i will have to run the code and close it to see the difference on my designer window how can i solve that?

If it is dependency property then use:

public static readonly DependencyProperty CountProperty =
                DependencyProperty.Register("Count", typeof(int), typeof(YourClass), new UIPropertyMetadata(OnCountChanged));

public int Value
{
       get { return (int)GetValue(CountProperty); }
       set { SetValue(CountProperty, value); }
}

private static void OnCountChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
{
       YourClass cp = obj as YourClass;
       MethodToExecute();
}

You can subscribe to the NotifyPropertyChanged event:

in your constructor put the following:

this.NotifyPropertyChanged += (sender, eventargs) =>
{
   if (eventargs.PropertyName == "Count")
      this.CountChanged();
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM