我有一個有兩個主要區域的窗口。一個是ScrollViewer內部的TextBox,另一個是TabControl。我想有圍繞當前具有焦點的部分紅色邊框,所以我寫了下面的代碼做在WPF中,如何在具有焦點的窗口部分放置邊框?
的XAML
<ScrollViewer BorderBrush="Red"
BorderThickness="0"
GotFocus="Border_GotFocus"
LostFocus="Border_LostFocus">
<TextBox/>
</ScrollViewer>
<TabControl BorderBrush="Red"
BorderThickness="0"
GotFocus="Border_GotFocus"
LostFocus="Border_LostFocus">
</TabControl>
代碼
private void Border_LostFocus(object sender, RoutedEventArgs e)
{
var control = sender as Control;
if (control != null)
{
control.BorderThickness = new Thickness(0);
}
}
private void Border_GotFocus(object sender, RoutedEventArgs e)
{
var control = sender as Control;
if (control != null)
{
control.BorderThickness = new Thickness(2);
}
}
的問題是,如果我點擊TextBox,它不會更新ScrollViewer的邊框。如果我點擊TabControl中的一個Tab,它會更新邊框,以便我可以看到邊框,但是當我點擊其他地方時不會「移除」邊框。有沒有更好的方法來做到這一點?
這是很好的作品。謝謝。 – juharr 2010-06-24 04:09:30