开发者

How can I call a function according to any data change in WPF (C#) page?

开发者 https://www.devze.com 2023-01-12 16:47 出处:网络
I n开发者_高级运维eed to do call a function according to any data change in WPF (C#) page, that is if any of the data of the current page is changed such as Textbox, Radiobutton, Checkbox,Combobox, Da

I n开发者_高级运维eed to do call a function according to any data change in WPF (C#) page, that is if any of the data of the current page is changed such as Textbox, Radiobutton, Checkbox, Combobox, Datagrid etc then I need to call a function how can I do it without using different event ?


You can use dependency properties in your code and bind to it from your control. That will allow changes in your control or your code to automatically updated.


Implement INotifyPropertyChanged in the class, Raise the property changed notification in all properties setters.

HTH


If you put this in your code behind it will call Handler for every event

If u put this after InitializeComponent

InitializeComponent();

foreach (RoutedEvent event in EventManager.GetRoutedEvents())
{
    AddHandler(event, new RoutedEventHandler(Handler), true);
}

You can have it execute the Handler method. This will attach it to every event.

void Handler(object sender, RoutedEventArgs e) { Title = e.ToString(); }

Alternatively you can just set the events that you want for example text changed to the same method, or you can use a linq query or some logic in the foreach loop to only attach to certain events.

Hope this helps

0

精彩评论

暂无评论...
验证码 换一张
取 消