Silverlight 中的 AutoCompleteBox 和 INotifyDataErrorInfo
有没有人成功应用 INotifyDataErrorInfo 接口并绑定到 AutoCompleteBox。我已经尝试过这个,但没有得到任何回应。该控件不像其他控件那样响应,即带有红色边框和警告工具提示。它还不会使验证摘要控件显示错误。
我已经成功地设置了标准的 TextBoxes 和 DatePickers,它们的行为完美地符合互联网上人们善意提供的许多示例。
如果有一个答案可以保证我的屏幕的一致性,那就太好了,因为我想简单地绑定到 INotifyDataErrorInfo 附带的 HasErrors 属性,以便在准备保存时启用一个按钮,如果没有,我就无法做到这一点额外的代码来检查这些框是否正确。
目前,我通过使用 MVVMLight EventToCommand 绑定并注册 LostFocus 事件来以不同的方式对待这些。
<sdk:AutoCompleteBox x:Name="TransferTypeTextBox" SelectedItem="{Binding Path=SelectedTransferType, Mode=TwoWay, ValidatesOnNotifyDataErrors=True, NotifyOnValidationError=True}" ItemsSource="{Binding Path=TransferTypes}" IsTextCompletionEnabled="True" Grid.Row="1" Grid.Column="1" Margin="0,3" Width="238" HorizontalAlignment="Left" FontFamily="/PtrInput_Silverlight;component/Fonts/Fonts.zip#Calibri" FontSize="13.333">
<i:Interaction.Triggers>
<i:EventTrigger EventName="LostFocus">
<cmd:EventToCommand Command="{Binding TransferTypeLostFocusCommand}" PassEventArgsToCommand="True"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</sdk:AutoCompleteBox>
然后,在 ViewModel 中,我将 RoutedEventArgs.OriginalSource 转换为 TextBox 并获取文本,如下所示,防止用户离开该框,除非该框为空或与框列表中的项目匹配:-
private void OnTransferTypeLostFocus(RoutedEventArgs e)
{
System.Windows.Controls.TextBox box = (System.Windows.Controls.TextBox)e.OriginalSource;
// If user inputs text but doesn't select one item, show message.
if (this.Ptr.TransferType == null && !string.IsNullOrEmpty(box.Text))
{
MessageBox.Show("That is not a valid entry for Transfer Type", "Transfer type", MessageBoxButton.OK);
box.Focus();
}
}
Has anyone successfully applied the INotifyDataErrorInfo interface and bound to an AutoCompleteBox. I've tried this but I get no response. The control does not respond as other controls i.e. with a red border and a warning tooltip. It also does not make the Validation Summary control display with its error.
I have successfully set up standard TextBoxes and DatePickers and these behave perfectly as per the many examples kindly provided by people on the internet.
it would be good if there was an answer to this for the consistency of my screen, also because i would like to simply bind to the HasErrors property that comes with INotifyDataErrorInfo to enable a button when ready to save and I can't do this without extra code to check that these boxes are correct.
At the moment I treat these differently by using an MVVMLight EventToCommand binding and registering the LostFocus event.
<sdk:AutoCompleteBox x:Name="TransferTypeTextBox" SelectedItem="{Binding Path=SelectedTransferType, Mode=TwoWay, ValidatesOnNotifyDataErrors=True, NotifyOnValidationError=True}" ItemsSource="{Binding Path=TransferTypes}" IsTextCompletionEnabled="True" Grid.Row="1" Grid.Column="1" Margin="0,3" Width="238" HorizontalAlignment="Left" FontFamily="/PtrInput_Silverlight;component/Fonts/Fonts.zip#Calibri" FontSize="13.333">
<i:Interaction.Triggers>
<i:EventTrigger EventName="LostFocus">
<cmd:EventToCommand Command="{Binding TransferTypeLostFocusCommand}" PassEventArgsToCommand="True"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</sdk:AutoCompleteBox>
In the ViewModel I then cast the RoutedEventArgs.OriginalSource to a TextBox and get the text like so, preventing the user from leaving the box unless it is empty or matching an item in the box's list: -
private void OnTransferTypeLostFocus(RoutedEventArgs e)
{
System.Windows.Controls.TextBox box = (System.Windows.Controls.TextBox)e.OriginalSource;
// If user inputs text but doesn't select one item, show message.
if (this.Ptr.TransferType == null && !string.IsNullOrEmpty(box.Text))
{
MessageBox.Show("That is not a valid entry for Transfer Type", "Transfer type", MessageBoxButton.OK);
box.Focus();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我试图写出尽可能简单的例子。
我的模型观察属性 SearchText 的变化并更新验证属性。
而xaml:
该控件有红色边框,当模型出现错误时该按钮被禁用。
I've tried to write as simple example as I can.
My model observe changes of the property SearchText and update validation properties.
And xaml:
The control has a red border and the button is disabled when the model has errors.