[英]Bubbling WPF control event in C#
I have a custom control that essentially wraps a ComboBox
and a TextBox
into a custom AutoCompleteTextBox
. 我有一个自定义控件,该
TextBox
实际上将ComboBox
和TextBox
包装到自定义AutoCompleteTextBox
。 I am attempting to bubble the TextBox.TextChanged
event up to my custom control AutoCompleteTextBox
so I can subscribe to the event as you would with any other event from a WPF by declaring the method to use for the subscriber in the XAML and instantiating that method in the code behind. 我试图将
TextBox.TextChanged
事件冒泡到我的自定义控件AutoCompleteTextBox
以便通过在XAML中声明要用于订阅服务器的方法并实例化该方法,从而像从WPF中订阅任何其他事件一样订阅该事件。后面的代码。
How do I make the TextBox.TextChanged
event trigger a new event for AutoCompleteTextBox
called, say, TextChanged so I can subscribe to AutoCompleteTextBox.TextChanged
? 如何使
TextBox.TextChanged
事件为AutoCompleteTextBox
触发一个称为TextChanged的新事件,以便我可以订阅AutoCompleteTextBox.TextChanged
?
I have found a lot on MSDN about it but am having an issue relating it to my instance. 我在MSDN上找到了很多有关它的信息,但是遇到了与我的实例有关的问题。 Any help would be appreciated
任何帮助,将不胜感激
The following is a stripped down version of the class that should provide all the necessary components related to what the class is the implementation of the TextBox.TextChanged
event to trigger from. 下面是该类的精简版本,该类应提供与该类是要触发的
TextBox.TextChanged
事件的实现相关的所有必要组件。
public partial class AutoCompleteTextBox : Canvas
{
private VisualCollection controls;
public TextBox textBox;
private ComboBox comboBox;
public AutoCompleteTextBox()
{
controls = new VisualCollection(this);
// set up the text box and the combo box
comboBox = new ComboBox();
comboBox.IsSynchronizedWithCurrentItem = true;
comboBox.IsTabStop = true;
comboBox.TabIndex = 1;
textBox = new TextBox();
textBox.TextWrapping = TextWrapping;
if (MaxLength > 0)
{
textBox.MaxLength = MaxLength;
}
textBox.VerticalScrollBarVisibility = ScrollBarVisibility.Auto;
textBox.VerticalContentAlignment = TextWrapping == System.Windows.TextWrapping.NoWrap ? System.Windows.VerticalAlignment.Center : System.Windows.VerticalAlignment.Top;
textBox.TextChanged += new TextChangedEventHandler(textBox_TextChanged);
textBox.TabIndex = 0;
controls.Add(comboBox);
controls.Add(textBox);
}
public int MaxLength { get; set; }
private void textBox_TextChanged(object sender, TextChangedEventArgs e)
{
}
}
Add an new event to your control / class: 向您的控件/类添加一个新事件:
public event EventHandler<EventArgs> MyTextChanged;
In textBox_TextChanged
do this: 在
textBox_TextChanged
执行以下操作:
if (MyTextChanged != null)
{
MyTextChanged(this, EventArgs.Empty);
}
This can than be enhanced with your custom event args if you would like, etc. 如果愿意,可以使用自定义事件args进行增强,等等。
If just named it MyTextChanged to mark what the new event is. 如果仅将其命名为MyTextChanged,以标记新事件是什么。 You of course can name it TextChanged .
您当然可以将其命名为TextChanged 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.