简体   繁体   English

TextBox的样式,该样式应在聚焦时关闭样式更改

[英]Style for a TextBox which should turn off style changes when focused

I have a simple style for my textboxes: 我的文本框有一个简单的样式:

<Style x:Key="PortalFocusVisualStyle" TargetType="TextBox">
    <!--<Setter Property="Focusable" Value="False"/>-->
    <Setter Property="BorderBrush">
        <Setter.Value>
            <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                <GradientStop Color="#7FFFFFFF" Offset="1"/>
                <GradientStop Color="#3FFFFFFF"/>
            </LinearGradientBrush>
        </Setter.Value>
    </Setter>
    <Setter Property="Background">
        <Setter.Value>
            <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                <GradientStop Color="#0CFFFFFF" Offset="0"/>
                <GradientStop Color="#26FFFFFF" Offset="1"/>
            </LinearGradientBrush>
        </Setter.Value>
    </Setter>
</Style>

This is what all my textboxes should look like. 这是我所有文本框的外观。 And by the way, PasswordBoxes if possible, too, but I can also do the a new style the same way for them. 顺便说一下,如果可能的话,也可以使用PasswordBoxes,但是我也可以用相同的方式来制作新样式。

When the user clicks the textbox, the top border dissappears nearly and looks strange. 当用户单击文本框时,顶部边框几乎消失并且看起来很奇怪。 For Windows 7 it seems this is default behaviour. 对于Windows 7,这似乎是默认行为。 But I don't want this, the textbox (or passwordbox) should look exactly the same now matter if it is focused or not. 但是我不希望这样,文本框(或密码框)现在看起来应该完全一样,无论是否集中。

So my idea was to use this style for both style and focusvisualstyle like this: 所以我的想法是对样式和focusvisualstyle都使用这种样式,如下所示:

My problem is still the focused textbox does not look the same. 我的问题仍然是集中的文本框看起来不一样。

I tried to work with visualstates and added a template in a setter to my above style: 我尝试使用视觉状态,并在上述样式的setter中添加了一个模板:

    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="TextBox" >
                <Grid x:Name="RootElement" Background="{TemplateBinding Background}" >
                    <VisualStateManager.VisualStateGroups>
                        <VisualStateGroup x:Name="FocusStates">
                            <VisualState x:Name="Focused"/>
                            <VisualState x:Name="Unfocused"/>
                        </VisualStateGroup>
                    </VisualStateManager.VisualStateGroups>
               </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>

But this is wrong somehow, I see an empty box only. 但这是错误的,我只看到一个空盒子。 I don't know what I need to add to the grid or if the grid is a good idea, I cannot add Visualstates to the template at all, could anybody please help me out? 我不知道我需要添加什么到网格中,或者如果网格是一个好主意,我根本无法将Visualstates添加到模板中,有人可以帮我吗?

Or maybe there is an easier solution? 也许有更简单的解决方案? I want my textbox and passwordbox have the above style with that background and border from above, and when it is focused, it should look the same and not change the borders etc... 我希望我的文本框和密码框具有上面的样式以及从上方的背景和边框,并且当焦点对准时,它应该看起来相同并且不更改边框等。

Thanks a lot! 非常感谢! Eric 埃里克

This is what you need to do: 这是您需要做的:

<Style TargetType="{x:Type TextBox}">
        <Setter Property="Margin" Value="0" />
        <Setter Property="Padding" Value="0" />
        <Setter Property="FocusVisualStyle" Value="{x:Null}" />
        <Setter Property="BorderBrush">
            <Setter.Value>
                <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                    <GradientStop Color="#7FFFFFFF" Offset="1"/>
                    <GradientStop Color="#3FFFFFFF"/>
                </LinearGradientBrush>
            </Setter.Value>
        </Setter>
        <Setter Property="Background">
            <Setter.Value>
                <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                    <GradientStop Color="#0CFFFFFF" Offset="0"/>
                    <GradientStop Color="#26FFFFFF" Offset="1"/>
                </LinearGradientBrush>
            </Setter.Value>
        </Setter>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type TextBox}">
                    <Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}">
                        <ScrollViewer Margin="0" x:Name="PART_ContentHost"/>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

The Scrollviewer is the actual content host for the textbox template. Scrollviewer是文本框模板的实际内容宿主。 And the secret here is to set the FocusVisualStyle to null. 这里的秘密是将FocusVisualStyle设置为null。 This will give you a consistent look across all platforms. 这将使您在所有平台上保持一致的外观。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM