Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 619795
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T18:41:37+00:00 2026-05-13T18:41:37+00:00

I have a nested TreeView where I bind the doubleclick event on each item

  • 0

I have a nested TreeView where I bind the doubleclick event on each item so that the text of the node is changed to an editable textbox. I then use the lostFocus eventhandler to remove the textbox and restore the text.

  void treeViewItemWithMenu_MouseDoubleClick(object sender, MouseButtonEventArgs e)
    {
        if (selected == e.Source)
        {
            TextBox tb = new TextBox();
            tb.Text = this.Header.ToString();
            tb.Focus();
            tb.LostFocus += new RoutedEventHandler(tb_LostFocus);
            this.Header = tb;
            var a = e.OriginalSource;
            e.Handled = true;
        }
    }


    void tb_LostFocus(object sender, RoutedEventArgs e)
    {
        this.Header = ((TextBox)(this.Header)).Text;
    }

Unfortantly it does not seem like the lostFocus event is working correctly. When I click outside the textbox, it does not fire at all. I can even doubleclick on another node and it goes into edit mode (ie becomes a text box) while the first textbox seems to still have focus. Lost focus does not fire untill i start going back and forth between two text boxes.

I’m using vs2010 rc with project set to .net 3.5.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T18:41:38+00:00Added an answer on May 13, 2026 at 6:41 pm

    Oh my! Pardon my saying so, but every regular WPF user who has read your question has probably shaken their head in pity. We feel sad for you because you haven’t yet learned the true “Zen of WPF”, which is leveraging data binding, templates and triggers to make your UI dynamic rather than doing it the old-fashioned way. WPF is beautiful because of this ability. It also makes things “1000%” easier.

    I recommend you change your UI to use a Trigger on your TreeViewItem to replace your HeaderTemplate based on a property you define in the TreeViewItem. Set this property true when the item is double-clicked. Set it false when IsKeyboardFocusWithin goes false (you can override metadata and add a PropertyChangedCallback for this).

    As far as your LostFocus problem goes, I suspect your problem is that you have multiple focus scopes or it is a bug in the RC. Without seeing your XAML I can’t say much more than that.

    Additional details on doing this the “WPF way”

    Here are some of the details on how to implement this using an attached property, triggers and templates.

    Your templates can be as simple or as complex as you want. Here’s simple:

    <DataTemplate x:Key="NormalTemplate">
      <ContentPresenter />
    </DataTemplate>
    
    <DataTemplate x:Key="TextBoxTemplate">
      <TextBox Text="{Binding}" />
    </DataTemplate>
    

    Here is what your style would look like:

    <Style TargetType="TreeViewItem">
      <Setter Property="HeaderTemplate" Value="{StaticResource NormalTemplate}" />
      <Trigger Property="local:MyWindowClass.ShowTextBox" Value="true">
        <Setter Property="HeaderTemplate" Value="{StaticResource TextBoxTemplate}" />
      </Trigger>
    </Style>
    

    The attached property “ShowTextBox” can be created in MyWindowClass using the “propa” snippet – just type “propa” and hit tab, then fill in the blanks.

    To switch the item to show the textbox, just:

    SetShowTextBox(item, true);
    

    To switch it back:

    SetShowTextBox(item, false);
    

    I hope this helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 383k
  • Answers 384k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The largest non-overflowing integer in PHP is stored in the… May 14, 2026 at 11:00 pm
  • Editorial Team
    Editorial Team added an answer Hey I have the same problem, I am evaluating if… May 14, 2026 at 11:00 pm
  • Editorial Team
    Editorial Team added an answer As soon as you say "I create a table per… May 14, 2026 at 11:00 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.