ItemsControl和焦点问题

时间:2010-05-05 18:47:57

标签: wpf focus itemscontrol

我有一个ItemsControl,它给了我一些问题。它有一个DataTemplate,它包含一个绑定到代码隐藏中的属性的TextBox。当我按下Enter键时,会在属性中插入一个新元素。发生这种情况后,datatemplate中项目的焦点应该向下移动ItemsControl中的一个项目(以编程方式完成)。但事实并非如此。相反,它改变了两个要素。我在控件中放置了一个递归的可视树读取器方法,试图弄清楚发生了什么,看起来像是缺少一个可视元素。但是,我不确定是什么导致这个或如何解决它。任何帮助将非常感谢。谢谢!

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;
        }
    }
}

另外,这是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>

干杯,

安德鲁

1 个答案:

答案 0 :(得分:1)

我发现了问题。显然,我需要调用this.UpdateLayout(),以便在更改焦点元素之前重新绘制可视树。