WPF中焦点的问题
在WPF中用Popup来做窗口,但是在切换窗口的时候窗口不能失去焦点,所有的窗口都显示是得到了焦点,但焦点却在系统其他窗口之上比如我打开两个自己的窗口,当其中一个前端显示时,它调用了GotFocus,但另一个窗口并没有LostFocus。。。这有可能是什么原因造成的呢?望了解的人给予回答! --------------------编程问答-------------------- --------------------编程问答-------------------- 在WPF中,Popup Control是一个不可见的Block,它的PopupRoot才是在你的屏幕内显现的
Popup的Child Window是异步操作的,所以它有一个独立的Open和Close。如果你要设置Popup的Focus,请在Open里面设置,设置其内部内容的Focus,Popup的GotFocus会响应
Give u a sample
[code=XAML]
<StackPanel>
<Button Click="Button_Click_1"/>
<TextBlock>
<StackPanel>
<TextBlock Text="123"/>
<Popup Name="pop1">
<StackPanel>
<TextBox Name="txtBox" Text="456" Background="Yellow"/>
</StackPanel>
</Popup>
</StackPanel>
</TextBlock>
</StackPanel>
[/code]
public Window1()
{
InitializeComponent();
pop1.Opened += new EventHandler(pop1_Opened);
pop1.GotFocus +=new RoutedEventHandler(pop1_GotFocus);
}
void pop1_Opened(object sender, EventArgs e)
{
Popup p = (Popup)sender;
StackPanel panel = p.Child as StackPanel;
TextBox tb = (TextBox)panel.FindName("txtBox");
tb.Focus();
}
void pop1_GotFocus(object sender, RoutedEventArgs e)
{
//GotFocus when tb.Focus();
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
pop1.IsOpen = true;
bool bRet = pop1.Focus(); // Return false, u can't directly set focus to Popup
}
Hope this helps
补充:.NET技术 , .NET技术前瞻