这是我的xaml结构
<StackPanel>
<m:TextBoxWithEllipsis IsEllipsisEnabled="True"
Name="A"
LostFocus="text_LostFocus"/>
<m:TextBoxWithEllipsis IsEllipsisEnabled="True"
Name="B"
LostFocus="text_LostFocus"/>
</StackPanel>=>这种结构可以循环更多。例如:
<StackPanel>
<m:TextBoxWithEllipsis IsEllipsisEnabled="True"
Name="A"
LostFocus="text_LostFocus"/>
<m:TextBoxWithEllipsis IsEllipsisEnabled="True"
Name="B"
LostFocus="text_LostFocus"/>
</StackPanel>
<StackPanel>
<m:TextBoxWithEllipsis IsEllipsisEnabled="True"
Name="A"
LostFocus="text_LostFocus"/>
<m:TextBoxWithEllipsis IsEllipsisEnabled="True"
Name="B"
LostFocus="text_LostFocus"/>
</StackPanel>在.cs文件中,我将事件丢失焦点定义为下面
private void text_LostFocus(object sender, RoutedEventArgs e)
{
TextBox textbox = ((TextBox)sender);
if (textbox.Text.Trim().Length == 0)
{
System.Windows.Forms.DialogResult result1 = System.Windows.Forms.MessageBox.Show("Empty string!", "Warning",
System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Exclamation);
textbox.Dispatcher.BeginInvoke((Action)(() => { textbox.Focus(); }));
return;
}
textbox.ScrollToHome();
}问题:如果有>= 2文本框具有值,则为空("")。
==>程序总是显示消息框=>,如果我单击OK按钮,它会显示另一个。它永远都会发生。我不能关闭程序。
problem :如果我有>= 2空文本框,并且与上面的问题相同,则为。如何更改函数text_LostFocus 以解决问题?
默认
发布于 2015-01-07 15:35:59
如果我是你,我不会用MessageBox。WPF有一个非常好的“绑定验证框架”(以看这儿作为一个非常好的教程)。否则,我将创建一个“警告”标签,位于关闭每个文本框:
<StackPanel>
<m:TextBoxWithEllipsis IsEllipsisEnabled="True"
Name="A"
LostFocus="text_LostFocus"/>
<TextBlock Name="AWarning" Foreground="Red" />
<m:TextBoxWithEllipsis IsEllipsisEnabled="True"
Name="B"
LostFocus="text_LostFocus"/>
<TextBlock Name="BWarning" Foreground="Red" />
</StackPanel>然后在代码隐藏中:
private void text_LostFocus(object sender, RoutedEventArgs e)
{
TextBox textBox = ((TextBox)sender);
TextBlock textBlock = FindName(String.Concat(textBox.Name, "Warning")) as TextBlock;
textBlock.Text = String.IsNullOrWhiteSpace(textBox.Text) ? "Empty string!" : String.Empty;
}https://stackoverflow.com/questions/27820278
复制相似问题