积极答复者
在 执行 ChildWindow 的 Closed 事件中 尝试调用其他控件的 Focus 方法 失败 可有解决方法?

问题
答案
-
你好,
我试验了一下好象没问题啊:
MainPage.xaml:
<StackPanel>
<TextBox x:Name="TextBox1"></TextBox>
<TextBox x:Name="TextBox2"></TextBox>
<controls:Calendar></controls:Calendar>
<Button Click="Button_Click" Width="100" Height="30" Content="Test"></Button>
</StackPanel>
MainPage.xaml.cs:
public MainPage()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
ChildWindow1 cw = new ChildWindow1();
cw.Closed +=
new EventHandler(cw_Closed);
cw.Show();
}
void cw_Closed(object sender, EventArgs e)
{
this.TextBox2.Focus();
}
Please remember to mark the replies as answers if they help and unmark them if they provide no help. Welcome to the All-In-One Code Framework http://cfx.codeplex.com/! If you have any feedback, please tell us.- 已标记为答案 zhang xiaolei 2009年11月6日 1:49
2009年11月5日 7:21
全部回复
-
在 Closed 事件中 要让 RootVisual 上面的某个自定义控件 根据 ChildWindow 中的用户输入结果 做出变化 并聚焦 调试过程中发现 RootVisual 上的自定义控件在调用了 Focus 后获得了焦点 但 瞬间又失去了 焦点被 ChildWindow 上第一个可以获得焦点的控件夺回了 想必是 ChildWindow 在执行消失的动画的时候 抢去了焦点
目前 用了 个 DispatcherTimer 设定1秒钟后执行 RootVisual 上的自定义控件 变化和聚焦 目的是为了 等ChildWindow 完全消失后再继续 以避免焦点被抢 问题是解决了 但是觉得不太好 有没有其他途径能解决这个问题 我是说让程序能自然运行下去 并且让 RootVisual 上面的 自定义控件获得焦点2009年11月5日 6:00 -
你好,
我试验了一下好象没问题啊:
MainPage.xaml:
<StackPanel>
<TextBox x:Name="TextBox1"></TextBox>
<TextBox x:Name="TextBox2"></TextBox>
<controls:Calendar></controls:Calendar>
<Button Click="Button_Click" Width="100" Height="30" Content="Test"></Button>
</StackPanel>
MainPage.xaml.cs:
public MainPage()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
ChildWindow1 cw = new ChildWindow1();
cw.Closed +=
new EventHandler(cw_Closed);
cw.Show();
}
void cw_Closed(object sender, EventArgs e)
{
this.TextBox2.Focus();
}
Please remember to mark the replies as answers if they help and unmark them if they provide no help. Welcome to the All-In-One Code Framework http://cfx.codeplex.com/! If you have any feedback, please tell us.- 已标记为答案 zhang xiaolei 2009年11月6日 1:49
2009年11月5日 7:21