2

我有一个 WPF 应用程序,其中包含构成 UI 的大量文本框和组合框。我有一个“保存”按钮,只要这些文本框或组合框的内容发生变化,我就想将其变为红色。是否有一个我可以处理的事件,这样我就不必写了

btnSave.Foreground = Brushes.Red

每个文本框的 _TextChanged 事件?

4

3 回答 3

3

You could loop through all textboxes on the form by looping through the form's child Controls, and bind the event in one swoop accordingly.

//-- This is a hair on the pseudo side, ChangeSavebuttonToRedHandler is an event 
//-- handler for your foreground change.. You can also use a lambda or whatever 
//-- you'd like.
foreach(var loChild in this.Controls)
{
    if(loChild is TextBox)
    {
        loChild.TextChanged += ChangeSaveButtonToRedHandler;
    }
}
于 2012-10-30T19:32:24.100 回答
1

There are plenty ways to do such a thing. The best thing is to understand "Bubbling and Tunneling in WPF" and the usage of "Attached events". Here are some references:

This link has all you need. http://msdn.microsoft.com/en-us/library/ms742806.aspx

http://www.wpfmentor.com/2008/11/understand-bubbling-and-tunnelling-in-5.html

Cheers

于 2012-10-30T19:33:58.877 回答
1

使用 Blend 创建行为。请参阅:使用 Blend 将可见性行为添加到用于 WPF 或 Silverlight 的 DataGrid

于 2012-10-30T20:09:56.460 回答