【问题标题】:XAML textbox change border color on focusXAML 文本框在焦点上更改边框颜色
【发布时间】:2016-07-22 23:10:41
【问题描述】:

问题


我目前遇到的问题是,在“IsFocused”属性上,我可以触发文本框更改的背景,但是当我想更改 BorderBrush 时它不起作用。

            <TextBox Padding="2" FontFamily="Sans Serif" Foreground="Red" FontSize="10px" FontWeight="Medium" Width="200" BorderThickness="2" VerticalAlignment="Center">
            <TextBox.Resources>
                <Style TargetType="{x:Type Border}">
                    <Setter Property="CornerRadius" Value="2"/>
                </Style>
            </TextBox.Resources>
            <TextBox.Style>
                <Style TargetType="{x:Type TextBox}">
                    <Setter Property="BorderBrush" Value="#858585"/>
                    <Style.Triggers>
                        <Trigger Property="IsFocused" Value="True">
                            <Setter Property="BorderBrush" Value="Red" />
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </TextBox.Style>
        </TextBox>

谁能看出这不起作用的原因?

编辑

我刚刚发现当我“右键单击”时它会变成红色?当用户点击文本框时,我希望它变为红色。

【问题讨论】:

    标签: wpf xaml textbox


    【解决方案1】:

    尝试使用该代码

            <TextBox 
    
                     Padding="2"  FontFamily="Sans Serif" 
                     Foreground="Red" FontSize="10px"
                     FontWeight="Medium" Width="200"  
                     VerticalAlignment="Center">
    
    
                <TextBox.Style>
    
                    <Style BasedOn="{x:Null}" TargetType="{x:Type TextBox}">
                        <Setter Property="BorderThickness" Value="2"/>
                        <Setter Property="Padding" Value="1"/>
                        <Setter Property="AllowDrop" Value="true"/>
                        <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate TargetType="{x:Type TextBox}">
                                    <Border x:Name="bg" BorderBrush="#FF825E5E" BorderThickness="1">
                                        <ScrollViewer x:Name="PART_ContentHost" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                                    </Border>
                                    <ControlTemplate.Triggers>
    
                                        <Trigger Property="IsMouseOver" Value="True">
                                            <Setter Property="BorderBrush" TargetName="bg" Value="Red"/>
                                            <Setter Property="BorderThickness" TargetName="bg" Value="2"/>
                                        </Trigger>
    
                                        <Trigger Property="IsFocused" Value="True">
                                            <Setter Property="BorderBrush" TargetName="bg" Value="Red"/>
                                            <Setter Property="BorderThickness" TargetName="bg" Value="2"/>
                                        </Trigger>
    
    
                                        <Trigger Property="IsFocused" Value="False">
                                            <Setter Property="BorderBrush" TargetName="bg" Value="#858585"/>
                                            <Setter Property="BorderThickness" TargetName="bg" Value="2"/>
                                        </Trigger>
    
                                        <Trigger Property="IsMouseOver" Value="False">
                                            <Setter Property="BorderBrush" TargetName="bg" Value="#858585"/>
                                            <Setter Property="BorderThickness" TargetName="bg" Value="2"/>
                                        </Trigger>
    
                                    </ControlTemplate.Triggers>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
    
                </TextBox.Style>
           </TextBox>
    

    【讨论】:

    • 不客气。如果不需要,也可以删除最后一个触发器:
    【解决方案2】:

    这是由植入文本框样式本身的视觉状态触发器引起的,它们会覆盖您添加的任何触发器,要更改边框的颜色,您必须更改样式,非常简单,只需按照以下步骤操作:

    第 1 步:在 blend 中打开您的项目,因为它更适合设计控件。

    第 2 步:向您的页面添加一个文本框。

    第 3 步:右键单击文本框并选择:“EditTemplate”\“Edit a Copy...”

    这将带您进入模板设计阶段。

    第 4 步:查看此图片:https://postimg.org/image/ocdn34is1/

    【讨论】:

      【解决方案3】:

      尝试使用 TextBox 的 GotFocus 和 LostFocus 事件并从后面的代码中完成。

      <TextBox x:Name="txtBox" ...
                   GotFocus="YourHandler1" LostFocus="YourHandler2"> ... </TextBox>
      

      然后您必须在 YourHandler1 中将边框颜色设置为红色,并在 YourHandler2 中将其设置回默认值。 (“发件人”将是您的 TextBox,所以这没什么大不了的。)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-06-29
        • 2022-11-27
        • 1970-01-01
        相关资源
        最近更新 更多