动态事件在WPF项目中不止一次起作用

时间:2016-12-14 23:11:22

标签: c# wpf

我已经将一个KeyDown事件添加到我在WPF项目中动态添加的TextBoxes中。我希望这个事件对任何TextBox运行一次。但是,当我按任意键时,此事件适用于TextBox的数量。如何阻止此事件再运行n-1?

动态事件定义

        TextBox addTB = new TextBox();
        addTB.VerticalScrollBarVisibility = ScrollBarVisibility.Auto;
        addTB.BorderThickness = new Thickness(0);
        addTB.BorderBrush = new SolidColorBrush(Colors.Gainsboro);
        addTB.VerticalContentAlignment = VerticalAlignment.Center;
        addTB.AcceptsReturn = true;
        addTB.TextWrapping = TextWrapping.Wrap;
        addTB.FontSize = 13;
        addTB.Text = toDo.toDo;
        addTB.Background = new SolidColorBrush(Color.FromRgb(64, 64, 64));
        addTB.Foreground = new SolidColorBrush(Colors.White);
        addTB.Padding = new Thickness(2, 0, 10, 2);
        AddHandler(TextBox.TextChangedEvent, new TextChangedEventHandler(addedText_Changed));
        //AddHandler(TextBox.GotFocusEvent, new RoutedEventHandler(addedText_PreviewGotKeyboardFocus));
        AddHandler(TextBox.PreviewKeyDownEvent, new KeyEventHandler(addedText_PreviewKeyDown));
        addTB.Tag = toDo.id;

事件

private void addedText_PreviewKeyDown(object sender, KeyEventArgs e)
    {

        if (e.Source is TextBox)
        {
            TextBox keyDownText = e.Source as TextBox;
            if (keyDownText.IsFocused)
            {
                if (e.Key.Equals(Key.Down))
                {                        
                    e.Handled = true;
                    foreach (TextBox tb in FindVisualChildren<TextBox>(mainWindow))
                    {
                        if (tb.Name != "textBox_toDo")
                        {
                            if (tb.Tag != null)
                            {
                                if ((int)tb.Tag > (int)keyDownText.Tag)
                                {
                                    tb.Focus();
                                    if (!string.IsNullOrEmpty(tb.Text))
                                    {
                                        tb.CaretIndex = tb.Text.Length;
                                    }
                                    return;
                                }
                            }
                        }
                    }
                    return;
                }
                else if (e.Key.Equals(Key.Up))
                {
                    e.Handled = true;
                    foreach (TextBox tb in FindVisualChildren<TextBox>(mainWindow).Reverse())
                    {
                        if (tb.Name != "textBox_toDo")
                        {
                            if (tb.Tag != null)
                            {
                                if ((int)tb.Tag < (int)keyDownText.Tag)
                                {
                                    tb.Focus();
                                    if (!string.IsNullOrEmpty(tb.Text))
                                    {

                                        tb.CaretIndex = tb.Text.Length;
                                    }
                                    return;
                                }
                                if ((int)tb.Tag == appData.toDoList.OrderBy(x => x.id).First().id)
                                {
                                    textBox_toDo.Focus();
                                    if (!string.IsNullOrEmpty(textBox_toDo.Text))
                                    {

                                        textBox_toDo.CaretIndex = textBox_toDo.Text.Length;
                                    }
                                    return;
                                }
                            }
                        }
                    }
                    return;

                }
                else if (e.Key.Equals(Key.F4))
                {                        
                    foreach (Image image in FindVisualChildren<Image>(mainWindow).Reverse())
                    {
                        if (image.Tag != null)
                        {
                            if (image.Name == "image_status" + keyDownText.Tag.ToString())
                            {
                                changeStatus(image);

                                //MouseButtonEventArgs ex = new MouseButtonEventArgs(Mouse.PrimaryDevice, 0, MouseButton.Left);
                                //ex.RoutedEvent = Mouse.MouseDownEvent;

                                //image.RaiseEvent(ex);

                                return;
                                // Or
                                // InputManager.Current.ProcessInput(ex);
                            }
                        }
                    }

                    return;
                } 
            }
        }
    }

2 个答案:

答案 0 :(得分:0)

尝试

AddHandler(addTB.TextChangedEvent , new TextChangedEventHandler(addedText_Changed));
AddHandler(addTB.PreviewKeyDownEvent, new KeyEventHandler(addedText_PreviewKeyDown));

看起来每次创建文本框时,都会注册文本框类的事件处理程序,而不是文本框对象(在您的情况下为addTB)本身,因此每个文本框都有多个注册。

答案 1 :(得分:0)

而不是这个,

AddHandler(TextBox.PreviewKeyDownEvent, new KeyEventHandler(addedText_PreviewKeyDown));

尝试添加直接处理程序,

 addTB.PreviewkeyDown += addTB_PreviewkeyDown;