我有一个自定义控件,基本上将ComboBox
和TextBox
包装到自定义AutoCompleteTextBox
中。我试图将TextBox.TextChanged
事件冒泡到我的自定义控件AutoCompleteTextBox
,所以我可以像使用WPF中的任何其他事件一样订阅事件,方法是声明用于XAML中订阅服务器的方法并在代码中实例化该方法背后。在C中冒泡WPF控件事件#
如何让TextBox.TextChanged
事件触发一个名为TextChanged的AutoCompleteTextBox
的新事件,以便我可以订阅AutoCompleteTextBox.TextChanged
?
我在MSDN上发现了很多关于它的问题,但是有一个问题将它与我的实例相关联。任何帮助,将不胜感激
以下是该类的精简版本,应该提供所有必需的组件相关的类是什么类的触发事件的执行从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)
{
}
}
我想你可以做路由事件(http://msdn.microsoft.com/en-us/library/ms742806(v=vs.110).aspx),因为我记得如果文本框是在同样的视觉三,你可以简单地在你的AutoCompleteTextBox构造函数中做:this.AddHandler(TextBox.TextChanged,new TextChangedEventHandler(yourMethodName));使用wpf自定义路由事件搜索教程 – iulian3000
嘿约翰,我给了这个镜头,但它不会接受textbox.TextChanged,因为它是一个TextChangedEventHandler而不是RoutedEventHandler。 –
试试这个:this.AddHandler(TextBox.TextChangedEvent,new TextChangedEventHandler(yourMethodName));我还没有输入正确,只是从VS复制粘贴。使用此处理程序,您将拦截画布中任何文本框中的任何文本更改事件。 – iulian3000