如何讓expander展開後,就Focus在子項目
說明:若將textbox1.Focus()直接寫在expander的expanded裡會回傳false,因為在展開時,該textbox1並未看的見,故無法focus();
作法:
1.建立一個boolean值,控制是否要focus。如beFocus = false;
2.於expander的Expanded事件中,將beFocus設為true;
3.於要被Focus的子項目之IsVisibleChanged事件中,撰寫若visible = true 則focus 自己,並將beFocus改回false。
Example
private bool beFocus = false;
private void expander1_Expanded(object sender, RoutedEventArgs e)
{
beFocus = true;
}
private void textbox1_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
{
if (((bool)e.NewValue == true) && beFocus) {
textbox1_.Focus();
beFocus = false;
}
}
沒有留言:
張貼留言