【问题标题】:ItemsControl and Focus IssueItemsControl 和焦点问题
【发布时间】:2010-05-05 18:47:57
【问题描述】:

我有一个 ItemsControl,它给我带来了问题。它有一个 DataTemplate,其中包含一个绑定到代码隐藏中的属性的 TextBox。当我按下 Enter 键时,一个新元素被插入到属性中。发生这种情况后,数据模板中项目的焦点应在 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>

干杯,

安德鲁

【问题讨论】:

  • 我在想,“也许有一些方法可以刷新 ItemsContol 之类的。”但是我还没有找到解决办法...

标签: wpf focus itemscontrol


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-22
    • 2011-03-07
    • 2016-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多