以下进一步说明.net 4.0代码的行为如下:
单击文本框以使其获得焦点,然后单击按钮:
这些错误或行为是否设计 - 如果以后有什么进一步的解释?
<Window x:Class="WpfApplication4.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="216,194,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="197,108,0,0" Name="textBox1" VerticalAlignment="Top" Width="120" />
</Grid>
</Window>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
textBox1.LostFocus += new RoutedEventHandler(handlelostfocus);
}
private void handlelostfocus(object sender, RoutedEventArgs e)
{
MessageBox.Show("handlelostfocus");
}
private void button1_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("click");
}
}
答案 0 :(得分:2)
在这种情况下,'click'永远不会发生,因为H.B.表示您通过显示模态消息框来中断UI /事件逻辑,因此按钮上永远不会出现鼠标按下事件。
尝试使用非模态窗口替换消息框,例如: new Window(){Width = 300,Height = 100,Title =“handlelostfocus”}。Show();
您将看到事件仍然发生,因为您没有在事件逻辑中间将焦点从主窗口中拉出来。
答案 1 :(得分:2)
将Button的ClickMode属性更改为“按”
<Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="216,194,0,0" ClickMode="Press" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" MouseUp="button1_MouseUp" MouseLeftButtonUp="button1_MouseLeftButtonUp" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="197,108,0,0" Name="textBox1" VerticalAlignment="Top" Width="120" />
答案 2 :(得分:1)
你打断了点击逻辑,只需点击鼠标按下鼠标,它就需要在Button
上连续连续;因此观察到的行为对我来说似乎很好。