Wpf Items控件和焦点问题
我有一个ItemsControl,它给了我一些问题。它有一个DataTemplate,其中包含一个绑定到代码隐藏中的属性的文本框。当我按Enter键时,一个新元素被插入到属性中。发生这种情况后,datatemplate中项目的焦点应向下移动ItemsControl中的一个项目(以编程方式完成)。然而,事实并非如此。相反,它由两个元素移动。我在控件中放了一个递归的可视化树读取器方法,试图弄清楚发生了什么,但似乎缺少了一个可视化元素。但是,我不确定是什么原因造成的,也不知道如何修复。任何帮助都将不胜感激。谢谢Wpf Items控件和焦点问题,wpf,focus,itemscontrol,Wpf,Focus,Itemscontrol,我有一个ItemsControl,它给了我一些问题。它有一个DataTemplate,其中包含一个绑定到代码隐藏中的属性的文本框。当我按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;
}
}
}
使用系统;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Input;
使用System.Windows.Media;
使用System.Collections.ObjectModel;
使用System.IO;
命名空间FunWithListBox.View
{
///
///EntryListView.xaml的交互逻辑
///
公共部分类EntryListView:UserControl
{
私有可观察收集项目列表;
公共可观察收集项目列表
{
获取{return itemList;}
设置{itemList=value;}
}
公共EntryListView()
{
初始化组件();
itemList=新的ObservableCollection();
Add(“列表中的第一个字符串”);
Add(“列表中的第二个字符串”);
Add(“列表中的第三个字符串”);
Add(“列表中的第四个字符串”);
Add(“列表中的第五个字符串”);
DataContext=this;
}
私有void UserControl\u KeyDown(对象发送方,KeyEventArgs e)
{
Type focType=Keyboard.FocusedElement.GetType();
if(focType==typeof(文本框))
{
if(e.Key==Key.Return)
{
TextBox tbxWithFocus=Keyboard.FocusedElement作为TextBox;
int index=itemList.IndexOf(tbxWithFocus.DataContext作为字符串);
string strotcursor=tbxWithFocus.Text.Substring(0,tbxWithFocus.CaretIndex);
字符串strPastCursor=tbxWithFocus.Text.Substring(tbxWithFocus.CaretIndex);
itemList[index]=strotcursor;
if(index==itemList.Count-1)
itemList.Add(strPastCursor);
其他的
itemList.Insert(索引+1,strPastCursor);
TextWriter tw=新的StreamWriter(“sampleVisualTree.txt”);
Write(getVisualTree(myItemsControl,2));
tw.Close();
tbxWithFocus.MoveFocus(新的遍历请求(FocusNavigationDirection.Down));
}
}
}
私有字符串getVisualTree(DependencyObject dpo,整数深度)
{
弦乐花环;
treeString=String.Empty.PadRight(深度,')+dpo.ToString();
for(int i=0;i
另外,这里是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>
干杯
安德鲁我发现了问题。显然,我需要调用此.UpdateLayout(),以便在更改聚焦元素之前重新绘制可视化树。我在想,‘也许有办法刷新ItemsContol或其他东西。’但我还没有找到解决方案。。。