2009-06-19 18 views
2

DataGridTemplateColumnのCellEditingTemplate内にAutoCompleteBoxがあり、前の列に一定数の文字が入力された後、自動フォーカスを試みています。DataGridTemplateColumn内のAutoCompleteBoxにフォーカスを設定

BeginInvokeメソッド(hereを参照)を使用して、コントロールがTextBoxである場合にのみ、シフトおよびキャレットを適切に設定することができました。このメソッドをAutoCompleteBoxで使用すると、キャレットは設定されず、コントロールにもフォーカスが得られないように見えます。

オートコンプリートボックスのフォーカスをsetCaretInCurrentCellメソッド内の参照を取得し、フォーカスを呼び出すことによって手動でフォーカスを設定しようとしましたが、それも動作しませんでした。

私は実際にこの列のAutoCompleteBox機能を失いましたが、データグリッドは、ユーザーがタブできない場合や自動的に次のフィールドにショーストッパーを使用する場合、データ入力用に最適化する必要があります。

ありがとうございました。

+0

あなたは、このための解決策を見つけるために管理するのですか?私は同様の問題に直面している。私を助けてくれますか? – Gopinath

答えて

0

グリッド/コントロールツリーの内部に入って物事を明示的にしようとするのではなく、現在の列設定を使用してください。ヘルプのデータ・テンプレート・サンプルをオフにリッピング

は、XAMLとbasiccodeをファイル次のとおりです。それだけで、カラムから移動一緒にハッキングされ

  • それはそれで長い余裕で完璧ではありません0〜1のスペースを押すことに基づいて、フォーカスを移動する条件に対する要件はより複雑です。

  • 私は双方向バインディングを設定していないので、編集が続くなど、列を移動する方法だけを見ています。

しかし、実行するとスペースに反応し、カーソルをオートコンプリートボックスに移動してテキストをハイライト表示し、入力を開始するとオートコンプリートドロップダウンがアクティブになります。したがって、原則として、現在の列を設定すると、後の動作を提供するように見えます。

A.

コード:

<UserControl x:Class="SilverlightApplication2.MainPage" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data" 
     xmlns:input="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Input" 
     xmlns:toolkit="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Toolkit" 
     xmlns:system="clr-namespace:System;assembly=mscorlib" 
     Width="400" Height="300"> 
     <ScrollViewer VerticalScrollBarVisibility="Auto" BorderThickness="0"> 
      <StackPanel Margin="10,10,10,10"> 

       <TextBlock Text="DataGrid with template column and custom alternating row backgrounds:"/> 
       <data:DataGrid x:Name="dataGrid5" 
       Height="125" Margin="0,5,0,10" 
       AutoGenerateColumns="False" 
       RowBackground="Azure" 
       AlternatingRowBackground="LightSteelBlue"> 
        <data:DataGrid.Columns> 
         <!-- Address Column --> 
         <data:DataGridTemplateColumn Header="Address" Width="300"> 
          <data:DataGridTemplateColumn.CellTemplate> 
           <DataTemplate> 
            <TextBlock Padding="5,0,5,0" Text="{Binding Address}"/> 
           </DataTemplate> 
          </data:DataGridTemplateColumn.CellTemplate> 
          <data:DataGridTemplateColumn.CellEditingTemplate> 
           <DataTemplate> 
            <TextBox Padding="5,0,5,0" Text="{Binding Address}"/> 
           </DataTemplate> 
          </data:DataGridTemplateColumn.CellEditingTemplate> 
         </data:DataGridTemplateColumn> 
         <!-- Name Column --> 
         <data:DataGridTemplateColumn Header="Name"> 
          <data:DataGridTemplateColumn.CellTemplate> 
           <DataTemplate> 
            <StackPanel Orientation="Horizontal"> 
             <TextBlock Padding="5,0,5,0" 
             Text="{Binding FirstName}"/> 
            </StackPanel> 
           </DataTemplate> 
          </data:DataGridTemplateColumn.CellTemplate> 
          <data:DataGridTemplateColumn.CellEditingTemplate> 
           <DataTemplate> 
            <StackPanel Orientation="Horizontal"> 
             <input:AutoCompleteBox Padding="5,0,5,0" 
             Text="{Binding FirstName}"> 
              <input:AutoCompleteBox.ItemsSource> 
               <toolkit:ObjectCollection> 
                <system:String>January</system:String> 
                <system:String>February</system:String> 
                <system:String>March</system:String> 
                <system:String>April</system:String> 
                <system:String>May</system:String> 
                <system:String>June</system:String> 
                <system:String>July</system:String> 
                <system:String>August</system:String> 
                <system:String>September</system:String> 
                <system:String>October</system:String> 
                <system:String>November</system:String> 
                <system:String>December</system:String> 
               </toolkit:ObjectCollection> 
              </input:AutoCompleteBox.ItemsSource> 
             </input:AutoCompleteBox> 
            </StackPanel> 
           </DataTemplate> 
          </data:DataGridTemplateColumn.CellEditingTemplate> 
         </data:DataGridTemplateColumn> 

        </data:DataGrid.Columns> 
       </data:DataGrid> 
       <Button Content="test"></Button> 
      </StackPanel> 
     </ScrollViewer> 

    </UserControl> 






using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Net; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Documents; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Animation; 
using System.Windows.Shapes; 

namespace SilverlightApplication2 
{ 
    public partial class MainPage : UserControl 
    { 
     public MainPage() 
     { 
      InitializeComponent(); 

      // Set the ItemsSource to autogenerate the columns. 

      dataGrid5.ItemsSource = Customer.GetSampleCustomerList(); 

      dataGrid5.KeyDown += new KeyEventHandler(dataGrid5_KeyDown); 

     } 

     void dataGrid5_KeyDown(object sender, KeyEventArgs e) 
     { 
      if (e.Key == Key.Space) 
      { 
       // move to next cell and start editing 
       DataGrid grd = (DataGrid)sender; 
       if (grd.CurrentColumn.DisplayIndex == 0) 
       { 
        // move to column 1 and start the edit 
        grd.CurrentColumn = grd.Columns[1]; 
       } 
      } 
     } 
    } 

    public class Customer 
    { 
     public String FirstName { get; set; } 
     public String LastName { get; set; } 
     public String Address { get; set; } 
     public Boolean IsNew { get; set; } 

     // A null value for IsSubscribed can indicate 
     // "no preference" or "no response". 
     public Boolean? IsSubscribed { get; set; } 

     public Customer(String firstName, String lastName, 
      String address, Boolean isNew, Boolean? isSubscribed) 
     { 
      this.FirstName = firstName; 
      this.LastName = lastName; 
      this.Address = address; 
      this.IsNew = isNew; 
      this.IsSubscribed = isSubscribed; 
     } 

     public static List<Customer> GetSampleCustomerList() 
     { 
      return new List<Customer>(new Customer[4] { 
       new Customer("A.", "Zero", 
        "12 North", 
        false, true), 
       new Customer("B.", "One", 
        "34 West", 
        false, false), 
       new Customer("C.", "Two", 
        "56 East", 
        true, null), 
       new Customer("D.", "Three", 
        "78 South", 
        true, true) 
      }); 
     } 
    } 

} 
関連する問題