c# - 如果在 "TextBox"上单击鼠标左键,则该元素会加速

标签 c# wpf

查看问题的操作:

  1. 创建 WPF 项目(我使用的是 .NET 7)
  2. 插入下面的代码

XAML:

<Window x:Class="TestApp.MainWindow"
        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:TestApp"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <TextBox HorizontalAlignment="Left" Margin="96,240,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="227" Height="97"/>
        <Button Content="Start" HorizontalAlignment="Left" Margin="400,240,0,0" VerticalAlignment="Top" Height="97" Width="160" Click="Button_Click"/>
        <Border Name="MyBorder" BorderBrush="Black" BorderThickness="1" Margin="96,45,0,0" Background="Black" VerticalAlignment="Top" HorizontalAlignment="Left" Height="131" Width="76"/>

    </Grid>
</Window>

C#:

using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace TestApp
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        async private void Button_Click(object sender, RoutedEventArgs e)
        {
            for (int i = 96; i <= 300; i++)
            {
                MyBorder.Margin = new Thickness(i,45,0,0);
                await Task.Delay(5);
            }
        }
    }
}

3) 单击“开始”按钮,指向“文本框”并按鼠标左键,可以看到“边框”开始移动得更快。

为什么会发生这种情况?如何解决这个问题?

问题视频: https://drive.google.com/file/d/1KWsp-ghJMnntW9KqbxcHxjtQ_fQ2tTbo/view?usp=share_link

最佳答案

尝试重现此问题没有成功。我确实注意到您缺少“使用 System.Threading.Tasks”,并且多次单击按钮时边框会出现故障。我建议在 for 循环之前禁用该按钮,并在 for 循环再次完成后启用它,如下所示:

XAML:

<Window x:Class="TestApp.MainWindow"
    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:TestApp"
    mc:Ignorable="d"
    Title="MainWindow" Height="450" Width="800">
<Grid>
    <TextBox HorizontalAlignment="Left" Margin="96,240,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="227" Height="97"/>
    <Button Name="start_button" Content="Start" HorizontalAlignment="Left" Margin="400,240,0,0" VerticalAlignment="Top" Height="97" Width="160" Click="Button_Click"/>
    <Border Name="MyBorder" BorderBrush="Black" BorderThickness="1" Margin="96,45,0,0" Background="Black" VerticalAlignment="Top" HorizontalAlignment="Left" Height="131" Width="76"/>
</Grid>

C#:

using System.Threading.Tasks;
using System.Windows;

namespace TestApp
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private async void Button_Click(object sender, RoutedEventArgs e)
        {
            start_button.IsEnabled = false;
            for (int i = 96; i <= 300; i++)
            {
                MyBorder.Margin = new Thickness(i,45,0,0);
                await Task.Delay(5);
            }
            start_button.IsEnabled = true;
        }
    }
}

也许您可以提供一些有关您正在使用的 Windows 版本以及您是否更改了项目中的任何其他文件的更多信息。 (也许尝试重新创建项目,看看是否会改变一些东西)

证明它对我有用:

-任务.延迟(5):

enter image description here

-任务.延迟(10):

enter image description here

关于c# - 如果在 "TextBox"上单击鼠标左键,则该元素会加速,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/77340032/

相关文章:

c# - 在 DataGrid-Column 中显示图像

c# - 在 WPF 的代码隐藏中更改字体样式

c# - 将集合绑定(bind)到 WPF 中的 ListBox

c# - Asp MVC 2 对现有电子邮件地址的远程验证,但编辑呢?

c# - WPF 按钮中的图像在运行时不可见

c# - 将数据集放入工作表的最快方法

c# - typeof() 有效,GetType() 在检索属性时无效

C# WPF 窗口在 foreach 循环中不更新

c# - MS Access 查询 "WHERE"异常

c# - 如何在不改变其他列单元格宽度的情况下将按钮的大小减小到 datagridview 按钮列单元格内的小尺寸