WPF文本框闪烁

时间:2019-07-17 20:00:01

标签: c# wpf xaml

我正在处理WPF应用程序。我想通过单击文本框来更改WPF应用程序中网格的可见性。当我更改网格的可见性时,文本框会闪烁。

闪烁的原因是什么?如何避免闪烁?

MainWindow.xaml

<Window x:Class="ChangeVisibility.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="ChangeVisbility" ResizeMode="NoResize" SizeToContent="WidthAndHeight" WindowStartupLocation="CenterScreen">
    <Window.Resources>
        <Style x:Key="TextBox" TargetType="{x:Type TextBox}">
            <Setter Property="Background" Value="#FF646464"/>
            <Setter Property="Foreground" Value="#FFFFFFFF"/>
            <Setter Property="BorderThickness" Value="0"/>
            <Setter Property="HorizontalContentAlignment" Value="Center"/>
            <Setter Property="Padding" Value="5,4,5,0"/>
            <Setter Property="UseLayoutRounding" Value="True"/>
            <Setter Property="FontFamily" Value="Arial"/>
            <Setter Property="FontSize" Value="22"/>
            <Setter Property="IsReadOnly" Value="True"/>
            <Setter Property="TextOptions.TextRenderingMode" Value="Grayscale"/>
            <Setter Property="Focusable" Value="False"/>
            <Setter Property="Cursor" Value="Arrow"/>
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Background" Value="#FF9B9B9B"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>
    <Grid>
        <Grid Name="Grid1" Width="256" Height="128" Background="#FFFFFFFF">
            <Grid.ColumnDefinitions>
                <ColumnDefinition/>
                <ColumnDefinition/>
                <ColumnDefinition/>
                <ColumnDefinition/>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition/>
                <RowDefinition/>
                <RowDefinition/>
                <RowDefinition/>
            </Grid.RowDefinitions>
            <TextBox Text="Input" MouseUp="Change" Grid.Row="2" Grid.RowSpan="1" Grid.Column="1" Grid.ColumnSpan="2" Style="{StaticResource TextBox}"/>
        </Grid>
        <Grid Name="Grid2" Width="256" Height="128" Background="#FFFFFFFF">
            <Grid.ColumnDefinitions>
                <ColumnDefinition/>
                <ColumnDefinition/>
                <ColumnDefinition/>
                <ColumnDefinition/>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition/>
                <RowDefinition/>
                <RowDefinition/>
                <RowDefinition/>
            </Grid.RowDefinitions>
            <TextBox Text="Output" MouseUp="Change" Grid.Row="2" Grid.RowSpan="1" Grid.Column="1" Grid.ColumnSpan="2" Style="{StaticResource TextBox}"/>
        </Grid>
    </Grid>
</Window>

MainWindow.xaml.cs

using System.Windows;

namespace ChangeVisibility
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        private void Change(object sender, RoutedEventArgs e)
        {
            if (Grid1.Visibility == Visibility.Visible)
            {
                Grid1.Visibility = Visibility.Hidden;
                Grid2.Visibility = Visibility.Visible;
            }
            else
            {
                Grid1.Visibility = Visibility.Visible;
                Grid2.Visibility = Visibility.Hidden;
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我猜是因为您的文本框样式。如您所见,您正在更改鼠标悬停时的背景颜色,一旦可见性代码触发,鼠标位置就会更改,背景颜色也将再次更改。

也尝试为“ IsMouseOver = False”设置背景色

 <Trigger Property="IsMouseOver" Value="False">
                <Setter Property="Background" Value="#FF9B9B9B"/>
  </Trigger>