我已經開發了一個自定義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)