我已经开发了一个自定义WPF控件(冒泡)验证事件:如何实现自定义WPF控件
public partial class PercentTextbox : UserControl, IDataErrorInfo, INotifyDataErrorInfo
而且我把控制以及一些其他控件的UserControl
内:
<UserControl x:Class="UserControlContainingPercentTextboxAndStuff" DataContext="Something" ...>
<Grid>
<mycontrols:PercentTextbox Value="{Binding MyPercentageValue, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, NotifyOnValidationError=True}" ... />
<TextBox ... />
<mycontrols:PercentTextbox ... />
<TextBox ... />
<TextBox ... />
</Grid>
</UserControl>
最后,我用另一个包装UserControl
作为对话框显示上面的UserControl
:
<UserControl ...>
<Grid>
<local:UserControlContainingPercentTextboxAndStuff ... />
<Button x:Name="SaveButton" Content="Save" ... />
<Button x:Name="CancelButton" Content="Cancel" ... />
</Grid>
</UserControl>
在后面的代码背后,我想要订阅所有验证错误,并在出现错误时禁用保存按钮。
Validation.AddErrorHandler(this, (sender, e) =>
{
SaveButton.IsEnabled = false;
Debug.WriteLine(e.Error);
});
我在想,如果我实现IDataErrorInfo
或INotifyDataErrorInfo
,WPF会神奇地处理的东西对我来说,并创建一个ValidationError事件(这会泡到UserControl
。但显然,我的思念这里的东西必不可少
我的问题是:怎么我有我的自定义控件PercentTextbox
来实现,以便使用它在任意地方,仍然可以得到某种起泡了验证信息,我可以在一个容器UserControl
使用(例如,禁用SaveButton)