KeyUp event is doubling using “mahapps.metro”

扶醉桌前 提交于 2019-12-10 10:46:02

问题


Complete project: https://uploadfiles.io/wz8ji

Follow code:

Code C#:

public partial class MainWindow : MetroWindow
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void Textbox_KeyUp(object sender, KeyEventArgs e)
    {
        if (textbox.Text != string.Empty)
        {
            this.ShowMessageAsync("This is the title", "Some message");
        }
    }
}

Code xaml:

<Controls:MetroWindow x:Class="Wpf.MainWindow"
        xmlns:Controls="http://metro.mahapps.com/winfx/xaml/controls"
        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"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <TabControl HorizontalAlignment="Left" Height="324" Margin="88,31,0,0" VerticalAlignment="Top" Width="605">
            <TabItem Header="TabItem">
                <Grid Background="#FFE5E5E5">
                    <TextBox 
                        x:Name="textbox"
                        HorizontalAlignment="Left"
                        Height="23"
                        Margin="10,10,0,0"
                        TextWrapping="Wrap"
                        Text="TextBox"
                        VerticalAlignment="Top"
                        Width="120"
                        KeyUp="Textbox_KeyUp"/>
                </Grid>
            </TabItem>
            <TabItem Header="TabItem">
                <Grid Background="#FFE5E5E5"/>
            </TabItem>
        </TabControl>
    </Grid>
</Controls:MetroWindow>

In my text box, when I quickly erase and type, the event is duplicated. When I delete and type "1" normally, everything is fine.The problem is when I delete and type any very fast character. When I do this quickly, the event is duplicated.

I also use this: http://mahapps.com/

I created another project without using "mahapps", it works right.

Here's how it's happening:

Press the backspace and release + 1 keys quickly.

Any Solution ?


回答1:


Well, I have submitted your code to different tests and the method is not duplicated...

int i = 0;
private void textBox1_KeyUp(object sender, KeyEventArgs e)
{
    if(((TextBox)sender).Text != string.Empty)
    {
        System.Diagnostics.Debug.WriteLine("Hello " + ++i);
    }
 }

Verify that another active control has no associated keyboard event, that may be the problem.

I have tested your code and it works well

As you told me in the chat, you quickly press backspace + 1, well, the error is that your code detects the pulsation of any key as long as the TextBox is not empty, so, what you must do is restrict the use of Back

private void Textbox_KeyUp(object sender, KeyEventArgs e)
{
    if (textbox.Text != string.Empty && e.Key != Key.Back)
     {
         this.ShowMessageAsync("This is the title", "Some message");
     }
}


来源:https://stackoverflow.com/questions/49594114/keyup-event-is-doubling-using-mahapps-metro

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!