开发者

Route an event into sibling control in WPF

I have a TextBox control for search, and a TreeView control below it. What I need is when I press the "Down Key ↓" while the focus on the search TextBox, I need to tell the TreeView "a Down Key has been pressed while the focus on开发者_C百科 you" ... I don't need to raise a KeyUp event on the TreeView or something, I need it to do the same action as if I pressed the Down Key while the focus on it!

Is this possible?


Can you do something like following:

private void TextBox_KeyUp(object sender, KeyEventArgs e)
{
    TreeView1.RaiseEvent(e);
}


protected override void OnStartup(StartupEventArgs e)
{
    EventManager.RegisterClassHandler(typeof(TextBox),
        TextBox.KeyUpEvent,
        new System.Windows.Input.KeyEventHandler(TextBox_KeyUp));

    base.OnStartup(e);
}

private void TextBox_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
{
    if (e.Key != System.Windows.Input.Key.Down) return;

    TreeViewItem newChild = new TreeViewItem();

    newChild.Header = "a Down Key has been pressed while the focus on you";

    Parent.Items.Add(newChild);
    e.Handled = true;
    //MessageBox.Show("Enter pressed");
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜