在 WPF 中,如何在窗口焦点部分周围添加边框?
我有一个有两个主要区域的窗口。一个是 ScrollViewer 内的 TextBox,另一个是 TabControl。我想在当前具有焦点的部分周围有一个红色边框,因此我编写了以下代码来执行该
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 中的选项卡,它会更新边框,以便我可以看到边框,但当我单击其他位置时不会“删除”它。有更好的方法来做到这一点吗?
I have a Window that has two primary areas. One is a TextBox inside of a ScrollViewer and the other is a TabControl. I want to have a red border around the part that currently has focus, so I wrote the following code to do that
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>
Code
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);
}
}
The problem is that if I click on the TextBox it does not update the border around the ScrollViewer. If I click on a Tab in the TabControl it updates the border so that I can see the border, but doesn't "remove" it when I click somewhere else. Is there some better way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,我强烈建议不要使用代码并将其全部保留在 XAML 中。
其次,我还建议使用
Border
来执行此操作。第三,我会在样式触发器中使用 IsKeyboardFocusedWithin 。
First off, I'd highly recommend not using code and keeping this all in XAML.
Secondly, I'd also recommend using a
Border
to do this.Third, I'd use IsKeyboardFocusedWithin in your style trigger.
接受的答案对我不起作用。然而我找到了一种不使用边框并且仍然获得相同效果的方法。
以下是文本框使用的样式。
我确实在风格上遇到了一些麻烦。最初我在触发器中声明了边框厚度和颜色。问题是,当框中有文本时,它似乎会跳跃。要避免跳跃效果,请默认声明边框厚度并将边框设置为透明,这样当焦点上的颜色发生变化时,就不会出现跳跃效果。
然后您只需将样式设置为文本框即可:
这就是您将看到的效果。
The accepted answer did not work for me. However I figured out a way without using a border and still getting the same effect.
Below is the style used for the text box.
I did run into some trouble with the style. Originally I declared the border thickness and the color in the Trigger. The problem with that is when there was text in the box it seemed to jump. To get around jumping effect is to declare a border thickness by default and set the border to transparent so when the color is changed on the focus there is not a jumping effect.
Then you just have to set the style to your textbox:
And this is the effect you will see.