2010-05-05 57 views
0

我有一個ItemsControl給我的問題。它有一個DataTemplate,它包含一個綁定到代碼隱藏屬性的TextBox。當我按Enter鍵時,一個新的元素被插入到屬性中。發生這種情況後,數據項中項目的焦點應該向下移動ItemsControl中的一個項目(以編程方式完成)。但是,它沒有。相反,它轉移了兩個元素。我在控件中放置了一個遞歸的可視樹閱讀器方法,試圖找出發生了什麼,看起來像缺少一個可視化元素。但是,我不確定是什麼原因造成的或如何修復它。任何幫助將非常感激。謝謝!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> 

乾杯,

安德魯

+0

我在想,'也許有一些方法可以刷新ItemsContol或其他東西。'但我還沒有找到解決方案... – Andrew 2010-05-05 19:17:00

回答

1

我發現這個問題。顯然,我需要調用this.UpdateLayout()以便在更改焦點元素之前重新繪製可視化樹。