WPF 文本框 & 边框 - 奇怪的调整大小行为
以下 XAML 生成一个文本框周围有奇怪行为的窗口:
<Window x:Class="WpfSandbox.CuriousExample"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="CuriousExample" Height="300" Width="300">
<DockPanel Margin="15">
<TextBox BorderThickness="1" BorderBrush="#FF000000"></TextBox>
</DockPanel>
</Window>
至少在我有限的测试期间,发生的情况是文本框以插入边框图案呈现(上/左为黑色,右/下为灰色)。 但是,当您调整大小到除原始位置之外的任何位置时,整个文本框边框将变为黑色。 每当您将窗口返回到表单首次加载时所具有的确切屏幕像素数时,它就会再次插入。
我猜这不是像素捕捉,因为我可以使用此代码轻松纠正问题:
<Window x:Class="WpfSandbox.CuriousExample"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="CuriousExample" Height="300" Width="300">
<DockPanel Margin="15">
<Border BorderThickness="1" BorderBrush="#FF000000">
<TextBox BorderThickness="0" ></TextBox>
</Border>
</DockPanel>
</Window>
有人愿意冒险解释我所看到的内容吗? 还是这一切都在我的脑海里?
就像我说的,上面的解决方法可以解决这个问题 - 只是试图了解这里发生了什么。
谢谢,
-斯科特
The following XAML produces a window with strange behavior around the textbox:
<Window x:Class="WpfSandbox.CuriousExample"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="CuriousExample" Height="300" Width="300">
<DockPanel Margin="15">
<TextBox BorderThickness="1" BorderBrush="#FF000000"></TextBox>
</DockPanel>
</Window>
What happens, at least during my limited testing, is that the textbox renders with an inset border pattern (top/left is black, right/bottom is grey). However, when you resize to any position except the original, the entire textbox border goes to black. Whenever you return the window to the exact number of on-screen pixels the form had when it first loaded, it's inset again.
I'm guessing it isn't pixel snapping as I can easily correct the problem with this code:
<Window x:Class="WpfSandbox.CuriousExample"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="CuriousExample" Height="300" Width="300">
<DockPanel Margin="15">
<Border BorderThickness="1" BorderBrush="#FF000000">
<TextBox BorderThickness="0" ></TextBox>
</Border>
</DockPanel>
</Window>
Anyone care to venture an explanation as to what I'm seeing? Or is it all in my head?
Like I said, the above workaround can resolve this problem - just trying to understand what is happening here.
Thanks,
-Scott
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
嗯...您遇到焦点问题了吗? 我加载了 Aero 主题,当 TextBox 具有焦点或鼠标悬停时,我看到您的 TextBox 插图。 当您添加第二个 TextBox 时,您可以清楚地看到这一点,如下所示:
Aero 的默认样式使用 ControlTemplate,它将 TextBox 的边框设置为使用 ListBoxChrome,当控件具有焦点或鼠标悬停时,ListBoxChrome 会设置一些额外的属性。
或者,Luna 主题的默认样式将包含的 Border 的 BorderBrush 直接绑定到 TemplateBinding,这意味着它始终受到尊重(以及为什么它在 XP/Luna 中工作,而不是在 2008 或 Vista 中工作)。
Hmm... are you running into a focus issue? I loaded the Aero theme, and I'm seeing your TextBox inset when the TextBox has focus or is moused-over. You can see this pretty clearly when you add a second TextBox like so:
The default Style for Aero uses a ControlTemplate which sets the TextBox's border to use the ListBoxChrome which looks to set some extra properties when the control has Focus or is moused over.
Alternately, the default Style for the Luna theme binds the containing Border's BorderBrush directly to the TemplateBinding, which means that this is always respected (and why it works in XP/Luna and not in 2008 or Vista).
您可以强制应用程序使用 vista 主题 (aero)
打开您的 app.xaml 并输入以下内容:
不要忘记将PresentationFramework.Aero 引用放入您的项目中。
有了这个,您将在 XP 中看到像在 Vista 中一样的应用程序。
You can force the application to use the vista theme (aero)
Open your app.xaml and put something like:
Don't forget to put the PresentationFramework.Aero reference into your project.
With this, you will se you application in XP like in Vista.