WPF 文本框 & 边框 - 奇怪的调整大小行为

发布于 2024-07-23 12:38:39 字数 1227 浏览 2 评论 0原文

以下 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

要走干脆点 2024-07-30 12:38:41

嗯...您遇到焦点问题了吗? 我加载了 Aero 主题,当 TextBox 具有焦点或鼠标悬停时,我看到您的 TextBox 插图。 当您添加第二个 TextBox 时,您可以清楚地看到这一点,如下所示:

<DockPanel Margin="15">
    <TextBox BorderThickness="1" BorderBrush="#FF000000"></TextBox>
    <TextBox BorderThickness="1" BorderBrush="#FF000000"></TextBox>
</DockPanel>

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:

<DockPanel Margin="15">
    <TextBox BorderThickness="1" BorderBrush="#FF000000"></TextBox>
    <TextBox BorderThickness="1" BorderBrush="#FF000000"></TextBox>
</DockPanel>

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).

人海汹涌 2024-07-30 12:38:40

您可以强制应用程序使用 vista 主题 (aero)

打开您的 app.xaml 并输入以下内容:

    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/PresentationFramework.Aero;V3.0.0.0;31bf3856ad364e35;component/themes/aero.normalcolor.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>

不要忘记将PresentationFramework.Aero 引用放入您的项目中。

有了这个,您将在 XP 中看到像在 Vista 中一样的应用程序。

You can force the application to use the vista theme (aero)

Open your app.xaml and put something like:

    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/PresentationFramework.Aero;V3.0.0.0;31bf3856ad364e35;component/themes/aero.normalcolor.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>

Don't forget to put the PresentationFramework.Aero reference into your project.

With this, you will se you application in XP like in Vista.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文