gpt4 book ai didi

c# - 如何以编程方式引发文本框 TextChangedEvent

转载 作者:行者123 更新时间:2023-11-30 22:05:26 27 4
gpt4 key购买 nike

我在 WPF 项目中使用 .net 4.5。我想提出一个 TextBox TextChangedEvent。这就是我所做的:

tb.RaiseEvent(new RoutedEventArgs(TextBox.TextChangedEvent));

我以前在 Button.ClickEvent 上做过同样类型的 RaiseEvent,我该怎么做呢?

这会产生如下错误:

Exception:Thrown: "Object of type 'System.Windows.RoutedEventArgs' cannot be converted to type 'System.Windows.Controls.TextChangedEventArgs'." (System.ArgumentException) A System.ArgumentException was thrown: "Object of type 'System.Windows.RoutedEventArgs' cannot be converted to type 'System.Windows.Controls.TextChangedEventArgs'."

[编辑]

实际更改的文本框文本由附加行为处理,如下所示。我想以编程方式引发事件的地方是另一个控件的附加行为。在后面的行为中,我确实有文本框对象。

public class textboxTextChangedBehavior : Behavior<TextBox>
{
protected override void OnAttached()
{
base.OnAttached();
AssociatedObject.TextChanged += OnTextChanged;
}

protected override void OnDetaching()
{
AssociatedObject.TextChanged -= OnTextChanged;
base.OnDetaching();
}

private void OnTextChanged(object sender, TextChangedEventArgs args)
{
var textBox = (sender as TextBox);
if (textBox != null)
{
//Populate ObservableCollection
}
}
}

我尝试发起事件的地方:

public class FindPopupBehavior : Behavior<Popup>
{
protected override void OnAttached()
{
AssociatedObject.Opened += _OpenFindPopup;
}

protected override void OnDetaching()
{
AssociatedObject.Opened -= _OpenFindPopup;
}

void _OpenFindPopup(object sender, EventArgs e)
{
//Fake a TextBox text changed event
if (TextBoxObject == null)
return;

TextBox tb = TextBoxObject as TextBox;

if (tb.Text == "")
return;

tb.RaiseEvent(new RoutedEventArgs(TextBox.TextChangedEvent));
}

public static readonly DependencyProperty TextBoxProperty =
DependencyProperty.Register("TextBoxObject", typeof(TextBox), typeof(FindPopupBehavior), new UIPropertyMetadata(null));

public object TextBoxObject
{
get { return (object)GetValue(TextBoxProperty); }
set { SetValue(TextBoxProperty, value); }
}
}

[EDIT2]

在关闭 ObservableCollection 时,文本框驻留在弹出窗口中,文本框文本仍然存在,尽管隐藏了。如果重新打开弹出窗口并且文本框有文本,则需要重新填充 ObservableCollection。它在 textchanged 行为中执行此填充。这就是为什么我想伪造一个事件。

最佳答案

手动引发 .NET 事件是不可取的,即使没有其他原因只是不需要。想一想……您真的不想发起事件。您不是在寻求事件的功能,那么为什么要引发它?

如果我对您的情况的理解正确,那么可以肯定的是,您实际上想要做的是执行代码的特定部分。因此,您的问题是因为该部分代码当前封装在事件处理程序中。但是,这并不意味着您必须引发该事件才能执行该代码。

相反,只需简单地将相关代码移动到一个方法中,您可以从事件处理程序从任何您想要的地方调用该方法,而不会引发任何不必要的事件:

private void OnTextChanged(object sender, TextChangedEventArgs args)
{
var textBox = (sender as TextBox);
if (textBox != null)
{
PopulateObservableCollection();
}
}

private void PopulateObservableCollection()
{
// Populate ObservableCollection
}

如果您特别需要访问 TextBox,请进一步描述您的情况...总有办法


更新>>>

从逻辑上考虑一下...您想调用此方法并且需要访问 Popup 中的 TextBox 并且每次 时都需要这样做code>Popup 被打开。那么为什么不直接处理 Popup.Opened Event 呢? ?:

private void PopupOpened(object sender, EventArgs e)
{
// Check TextBox and Populate ObservableCollection
}

关于c# - 如何以编程方式引发文本框 TextChangedEvent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24379803/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com