I have a usercontrol which may contain many different kind 开发者_JAVA百科of controls (e.g. button, textbox, combobox or checkbox). I need a generic way to track if anything has been changed. In most cases I don't care about the details (ie I don't need to know which textbox has changed). When I saw EventManager.RegisterClassEvent
, I thought this would be my solution, but soon I realize it's not. This problem is that it's fired too much. For example, if I have a button and a combobox in the same control, the button.click event is also fired when I hit the combobox dropdown.
In WinForms, I had to loop through all controls in the usercontrol and subscribe to appropriate event (e.g. TextBox.TextChanged, or ComboBox.SelectedIndexChanged), it's tedious but works. I hope there is a better and easier way to do it in WPF.
Routed events:
<UserControl x:Class="..." TextBoxBase.TextChanged="UserControl_TextChanged"
Selector.SelectionChanged="UserControl_SelectionChanged"
ToggleButton.Checked="UserControl_Checked"
ButtonBase.Click="UserControl_Click" />
You can Put this checking to validate the e.source in the UserControl_Click event to handle this
If(e.Source.GetType()==typeof(Button))
{
// your code
}
精彩评论