【问题标题】:Wpf textbox does not change borderbrush when trigger mouse over?触发鼠标悬停时,Wpf文本框不会更改边框?
【发布时间】:2016-03-23 06:15:52
【问题描述】:

在我的 WPF 应用程序中,我想在鼠标进入时更改 TextBox 的边框刷颜色,但它并没有将颜色更改为我想要的颜色,而是更改为天蓝色(我猜它是标准颜色)。

这是我的 XAML 代码:

 <Window x:Class="OnePlayApp.Views.LoginDialog"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:OnePlayApp.Views"
        WindowStyle="None"
        ResizeMode="NoResize"
        Title="LoginDialog" Height="350" Width="550" Foreground="Black">

    <Grid Background="#282828">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="24*"/>
            <ColumnDefinition Width="43*"/>
            <ColumnDefinition Width="43*"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="59*"/>
            <RowDefinition Height="50*"/>
            <RowDefinition Height="50*"/>
            <RowDefinition Height="191*"/>
        </Grid.RowDefinitions>

        <Image Margin="9,10,173,6" Source="/Resources/logo.png" Grid.ColumnSpan="2"/>


        <Label x:Name="AccountName" Content="Account name" Margin="6,10,1,13" Foreground="#FFAFADAD" HorizontalContentAlignment="Right" Grid.Row="1"/>
        <Label x:Name="Password" Content="Password&#xD;&#xA;" Margin="6,10,1,13" Foreground="#FFAFADAD" HorizontalContentAlignment="Right" Grid.Row="2"/>

        <TextBox x:Name="username" Margin="6,11,15,8" Background="#FF282828" Foreground="White" FontSize="15" Text="Ahsep12015@one.com" FontFamily="Yu Gothic UI Semibold" Grid.Column="1" Grid.Row="1" Grid.ColumnSpan="2" VerticalContentAlignment="Center" BorderThickness="10">
            <TextBox.Style>
                <Style TargetType="{x:Type TextBox}">
                    <Setter Property="BorderBrush" Value="Green" />
                    <Style.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="BorderBrush" Value="Red" />

                        </Trigger>
                    </Style.Triggers>
                </Style>
            </TextBox.Style>
        </TextBox>
        <PasswordBox x:Name="password" Margin="6,8,15,13" Background="#FF282828" Password="123456789" FontSize="18" Grid.Column="1" Grid.Row="2" Grid.ColumnSpan="2"/>

        <Button x:Name="LoginBtn" Content="Login" Margin="6,15,15,146" Click="LoginBtn_Click" FontSize="16" FontFamily="Yu Gothic UI Semibold" Foreground="White" Grid.Column="1" Grid.Row="3">
            <Button.Style>
                <Style TargetType="{x:Type Button}">
                    <Setter Property="Background" Value="#FFAFADAD"/>
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="{x:Type Button}">
                                <Border Background="{TemplateBinding Background}">
                                    <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
                                </Border>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                    <Style.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="Background" Value="#79B539"/>
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </Button.Style>
        </Button>
        <Button x:Name="CancelBtn" Content="Cancel" Margin="6,15,15,146" Click="CancelBtn_Click" FontSize="16" FontFamily="Yu Gothic UI Semibold" Foreground="White" Grid.Column="2" Grid.Row="3">
            <Button.Style>
                <Style TargetType="{x:Type Button}">
                    <Setter Property="Background" Value="#FFAFADAD"/>
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="{x:Type Button}">
                                <Border Background="{TemplateBinding Background}">
                                    <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
                                </Border>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                    <Style.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="Background" Value="#79B539"/>
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </Button.Style>

        </Button>



    </Grid>
    </Window>

【问题讨论】:

  • " - 谢谢。"
  • 嗨@petethepagan-gerbil 我试图上传我的 XAML 代码,但它没有出现在我的问题中?
  • 这行不通。最好的解决方案是为此文本框创建一个控件模板,并在控件模板中设置您的触发器
  • 这段代码对我来说很好用,边框变红(我将边框粗细设置为 10 以清楚地看到效果)(编辑显示代码待定)。
  • 嗨@petethepagan-gerbil。我的怎么没有?每当我将鼠标悬停在文本框上时,它就会变成天蓝色?

标签: c# wpf textbox


【解决方案1】:

我在 Windows 8 上遇到了同样的问题,我能够解决这个问题(这似乎是 WPF 中的一个错误)的唯一方法是使用 ControlTemplate。这是 Blend 提出的 TextBox 的标准控制模板。我更改了 BorderBrush 颜色,将其添加到我的全局 TextBox 样式中,效果很好。可能有更好的解决方案,但这对我有用。

<Style TargetType="{x:Type TextBox}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type TextBoxBase}">
                <Border x:Name="border" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
                    <ScrollViewer x:Name="PART_ContentHost" Focusable="False" HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden"/>
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsEnabled" Value="False">
                        <Setter Property="Opacity" TargetName="border" Value="0.56"/>
                    </Trigger>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter Property="BorderBrush" TargetName="border" Value="Red"/>
                    </Trigger>
                    <Trigger Property="IsKeyboardFocused" Value="True">
                        <Setter Property="BorderBrush" TargetName="border" Value="Orange"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-02-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多