将焦点设置在DataGridTemplateColumn中的AutoCompleteBox上

时间:2009-06-19 15:43:18

标签: silverlight silverlight-3.0 datagrid autocomplete

我在CellEditingTemplate中有一个DataCoridTemplateColumn的AutoCompleteBox,我试图在前一列中输入一定数量的字符后自动对焦。

我已经能够使用BeginInvoke方法(描述here)获得焦点转移和插入符合适当设置,但前提是控件是TextBox。当使用此方法与AutoCompleteBox时,插入符号不会设置,控件也不会获得焦点。

我试图通过在setCaretInCurrentCell方法中获取对它的引用并调用焦点但手动设置焦点来自动完成。

我真的想在这个专栏中使用AutoCompleteBox功能,但是数据网格需要针对数据输入进行优化,如果用户无法选项卡或自动将其带到下一个字段,那么它就会显示停止。

感谢。

1 个答案:

答案 0 :(得分:0)

我可能会对这个问题提出最奇怪的建议,但是不要试图进入网格/控制树并使用显式内容,而是使用当前列设置。

从XAML和基本代码中删除帮助文件中的数据模板示例如下:

长期以来并不完美:

  • 根据空间的按压,它只是在一起从第0列移动到1,你对移动焦点的条件要求更复杂。
  • 我没有设置双向绑定,因此编辑仍然存在等等,只是看了如何移动列。

但是当它运行时,它确实响应一个空格并将光标移动到自动完成框中,突出显示文本,如果我开始输入自动完成下拉菜单激活。因此,原则上,设置当前列似乎提供了您所追求的行为。

一个。

代码:

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

}
相关问题