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

  • SEARCH
  • Home
  • 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 789921
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T21:36:08+00:00 2026-05-14T21:36:08+00:00

I have an ItemsControl which is giving me issues. It has a DataTemplate, which

  • 0

I have an ItemsControl which is giving me issues. It has a DataTemplate, which contains a TextBox that is bound to a property in the code-behind. When I press the Enter key, a new element is inserted into the property. After this happens, the focus of the item in the datatemplate should shift down one item in the ItemsControl (done programatically). However, it doesn’t. Instead, it shifts by two elements. I put a recursive visual tree reader method in the control to try to figure out what’s going on, and it looks like one of the visual elements is missing. However, I’m not sure what’s causing this or how to fix it. Any help would be very much appreciated. Thanks!

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Collections.ObjectModel;
using System.IO;

namespace FunWithListBox.View
{
    /// <summary>
    /// Interaction logic for EntryListView.xaml
    /// </summary>
    public partial class EntryListView : UserControl
    {
        private ObservableCollection<string> itemList;
        public ObservableCollection<string> ItemList
        {
            get { return itemList; }
            set { itemList = value; }
        }

        public EntryListView()
        {
            InitializeComponent();

            itemList = new ObservableCollection<string>();
            itemList.Add("First string in the list.");
            itemList.Add("Second string in the list.");
            itemList.Add("Third string in the list.");
            itemList.Add("Fourth string in the list.");
            itemList.Add("Fifth string in the list.");

            DataContext = this;
        }

        private void UserControl_KeyDown(object sender, KeyEventArgs e)
        {
            Type focType = Keyboard.FocusedElement.GetType();

            if (focType == typeof(TextBox))
            {
                if (e.Key == Key.Return)
                {
                    TextBox tbxWithFocus = Keyboard.FocusedElement as TextBox;

                    int index = itemList.IndexOf(tbxWithFocus.DataContext as string);

                    string strToCursor = tbxWithFocus.Text.Substring(0, tbxWithFocus.CaretIndex);
                    string strPastCursor = tbxWithFocus.Text.Substring(tbxWithFocus.CaretIndex);

                    itemList[index] = strToCursor;

                    if (index == itemList.Count - 1)
                        itemList.Add(strPastCursor);
                    else
                        itemList.Insert(index + 1, strPastCursor);

                    TextWriter tw = new StreamWriter("sampleVisualTree.txt");
                    tw.Write(getVisualTree(myItemsControl, 2));
                    tw.Close();

                    tbxWithFocus.MoveFocus(new TraversalRequest(FocusNavigationDirection.Down));
                }
            }
        }

        private string getVisualTree(DependencyObject dpo, int depth)
        {
            string treeString;

            treeString = String.Empty.PadRight(depth, ' ') + dpo.ToString();

            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(dpo); i++)
                treeString +=
                    Environment.NewLine +
                    getVisualTree(VisualTreeHelper.GetChild(dpo, i), depth + 2);

            return treeString;
        }
    }
}

Also, here’s the xaml:

<UserControl x:Class="FunWithListBox.View.EntryListView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             Height="300" Width="300"
             KeyDown="UserControl_KeyDown">
    <Grid>
        <ItemsControl ItemsSource="{Binding Path=ItemList}"
                      x:Name="myItemsControl">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <TextBox Text="{Binding Mode=TwoWay,
                                            UpdateSourceTrigger=PropertyChanged,
                                            Path=.}"
                             BorderThickness="0" />
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </Grid>
</UserControl>

Cheers,

Andrew

  • 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-14T21:36:09+00:00Added an answer on May 14, 2026 at 9:36 pm

    I found the problem. Apparently, I needed to make a call to this.UpdateLayout() in order to have the visual tree redrawn before changing the focused element.

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

Sidebar

Related Questions

I have an ItemsControl that uses a DataTemplate. The DataTemplate contains a TextBox, which
I have an ItemsControl that contains items that each has its own DataTemplate .
I have an ItemsControl with a DataTemplate that has been defined. My ItemsControl definition
I have a WPF window which has a ItemsControl which contains list of User
In Silverlight, I have a DataTemplate which is bound to an object which contains
I have ItemsControl with DataTemplate that looks like this: I want to achieve effect
I have an ItemsControl that is bound to a collection of objects. Each object
I have a WPF ItemsControl that is bound to an ObservableCollection. The XAML: <ItemsControl
I have some XAML <ItemsControl Name=mItemsControl> <ItemsControl.ItemTemplate> <DataTemplate> <TextBox Text={Binding Mode=OneWay} KeyUp=TextBox_KeyUp/> </DataTemplate> </ItemsControl.ItemTemplate>
I have a xaml page that has an ItemsControl control on it. ItemsControl is

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.