This is a migrated thread and some comments may be shown as answers.

GridViewColumn.CellTemplate Focus BUG?

0 Answers 102 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Amitkumar
Top achievements
Rank 1
Amitkumar asked on 28 Feb 2012, 12:52 PM

Hi I am using Autocomplete box inside Grid view.
I can use Autocompletecompbox but my requirement is different. Now I can also use GridviewData Column but I want to set My focus fix in my Autocomplete box till data loading or data validating. Here I am attaching my xaml file and .cs file I am not able to attached project here so I cannot attaching.
In Conclusion issue is when add new row focus going back of first cell, it should go in Autocompletebox.


<UserControl xmlns:my="clr-namespace:SilverlightApplication1"  x:Class="SilverlightApplication1.MainPage"
    mc:Ignorable="d"
            xmlns:localVM="clr-namespace:SilverlightApplication1"
    d:DesignHeight="1174" d:DesignWidth="932" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk" xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation">
    <UserControl.Resources>
        <localVM:PassParameter x:Key="param"></localVM:PassParameter>
    </UserControl.Resources>
        <Grid x:Name="LayoutRoot" Background="White">
        
            <TextBox Height="23" Text="123" Margin="722,154,90,997" Name="textBox1" Width="120" />
            <!--<sdk:AutoCompleteBox IsTextCompletionEnabled="True" Height="27" MinimumPrefixLength="3" MinimumPopulateDelay="1000" HorizontalAlignment="Left" Margin="0,50,0,0" Name="autoCompleteBox1" VerticalAlignment="Top" Width="120" Populating="autoCompleteBox1_Populating" GotFocus="autoCompleteBox1_GotFocus" LostFocus="autoCompleteBox1_LostFocus"  ValueMemberBinding="{Binding Path=name}" Text="{Binding name1,Mode=TwoWay}" TabIndex="1">
                <sdk:AutoCompleteBox.ItemTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Path=name}" />
                    </DataTemplate>
                </sdk:AutoCompleteBox.ItemTemplate>
            </sdk:AutoCompleteBox>-->
 
 
            <sdk:Label Height="28" Content="number of time service call" HorizontalAlignment="Left" Margin="12,12,0,0" Name="label1" VerticalAlignment="Top" Width="168" />
            <sdk:Label Height="28" Content="0" HorizontalAlignment="Left" Margin="190,12,0,0" Name="label2" VerticalAlignment="Top" Width="120" />
            <Button  Content="reset counter" Height="23" HorizontalAlignment="Left" Margin="240,8,0,0" Name="button1" VerticalAlignment="Top" Width="121" Click="button1_Click" />
            <sdk:Label Height="28" HorizontalAlignment="Left" Margin="766,252,0,0" Name="label3" VerticalAlignment="Top" Width="120" />
 
            <!--<TextBox Height="23" HorizontalAlignment="Left" Margin="98,32,0,0" Name="textBox2" VerticalAlignment="Top" Width="120" Text="gogo" />
            <my:CustomAutoCompleteBox Visibility="Collapsed" Height="25" HorizontalAlignment="Left" Text="{Binding name1,Mode=TwoWay}" ServiceParameters="{Binding Parameters,Mode=TwoWay}" ServiceContractName=" SilverlightApplication1.ServiceReference1.IService1" MinimumPopulateDelay="1000" ServiceMethodName="GetRecords" MinimumPrefixLength="3" ServiceAbsoluteUri="http://localhost:51595/Service1.svc" IsTextCompletionEnabled="True" x:Name="acbCustomerID" ValueMemberBinding="{Binding Path=name}" IsMandatory="True" Width="100" ValuePropertyName="name"  AllowDrop="False"  Margin="0,20,0,0">
                <my:CustomAutoCompleteBox.ItemTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Path=name}" />
                    </DataTemplate>
                </my:CustomAutoCompleteBox.ItemTemplate>
            </my:CustomAutoCompleteBox>-->
        
 
        <telerik:RadGridView  CanUserSortColumns="False"  EditTriggers="Default,CellClick" CanUserInsertRows="True" ShowInsertRow="True" SelectionUnit="Cell" ItemsSource="{Binding DbStudents,Mode=TwoWay}" SelectedItem="{Binding SelectedRecord,Mode=TwoWay}"  HorizontalAlignment="Left" Margin="407,324,0,0" Name="radGridView1" VerticalAlignment="Top" AutoGenerateColumns="False" AddingNewDataItem="radGridView1_AddingNewDataItem" MouseLeftButtonUp="radGridView1_MouseLeftButtonUp">
            <telerik:RadGridView.Columns>
 
 
 
                <telerik:GridViewColumn Header="Name" Width="100">
                    <telerik:GridViewColumn.CellTemplate>
                        <DataTemplate>                       
                            <my:CustomAutoCompleteBox BorderThickness="0" IsMandatory="True"  HorizontalAlignment="Stretch" VerticalAlignment="Stretch"  Text="{Binding name,Mode=TwoWay}" ServiceParameters="{Binding Parameters,Mode=TwoWay,Source={StaticResource param}}" ServiceContractName="SilverlightApplication1.ServiceReference1.IService1" MinimumPopulateDelay="1000" ServiceMethodName="GetRecords" MinimumPrefixLength="3" ServiceAbsoluteUri="http://localhost:51595/Service1.svc" IsTextCompletionEnabled="True" x:Name="acbCustomerID1" ValueMemberBinding="{Binding Path=name}" ValuePropertyName="name"  AllowDrop="False">
                                <my:CustomAutoCompleteBox.ItemTemplate>
                                    <DataTemplate>                                     
                                        <TextBlock  Text="{Binding Path=name}"  HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>
                                    </DataTemplate>
                                </my:CustomAutoCompleteBox.ItemTemplate>                              
                            </my:CustomAutoCompleteBox>
                        </DataTemplate>
                    </telerik:GridViewColumn.CellTemplate>                                   
                     
                     
                </telerik:GridViewColumn>
                 
                <!--<telerik:GridViewDataColumn DataMemberBinding="{Binding Name}" Header="Name1">
                    <telerik:GridViewDataColumn.CellEditTemplate>
                        <DataTemplate>
                            <my:CustomAutoCompleteBox BorderThickness="0" IsMandatory="True"  HorizontalAlignment="Stretch" VerticalAlignment="Stretch"  Text="{Binding name,Mode=TwoWay}" ServiceParameters="{Binding Parameters,Mode=TwoWay,Source={StaticResource param}}" ServiceContractName="SilverlightApplication1.ServiceReference1.IService1" MinimumPopulateDelay="1000" ServiceMethodName="GetRecords" MinimumPrefixLength="3" ServiceAbsoluteUri="http://localhost:51595/Service1.svc" IsTextCompletionEnabled="True" x:Name="acbCustomerID2" ValueMemberBinding="{Binding Path=name}" ValuePropertyName="name"  AllowDrop="False">
                                <my:CustomAutoCompleteBox.ItemTemplate>
                                    <DataTemplate>
                                        <TextBlock  Text="{Binding Path=name}"  HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>
                                    </DataTemplate>
                                </my:CustomAutoCompleteBox.ItemTemplate>
                            </my:CustomAutoCompleteBox>
                        </DataTemplate>
                    </telerik:GridViewDataColumn.CellEditTemplate>
                </telerik:GridViewDataColumn>-->
 
 
                <telerik:GridViewDataColumn
            Header="Address"
            Width="SizeToHeader"
            DataMemberBinding="{Binding address,Mode=TwoWay}"
             />
                <telerik:GridViewDataColumn
            Header="Age"
            Width="SizeToHeader"
            DataMemberBinding="{Binding age,Mode=TwoWay}"
             />
            </telerik:RadGridView.Columns>
        </telerik:RadGridView>
    </Grid>
</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;
using System.ComponentModel;
using SilverlightApplication1.ServiceReference1;
using System.Collections.ObjectModel;
using System.Threading;
using Telerik.Windows.Controls.GridView;
using Telerik.Windows.Controls;
 
namespace SilverlightApplication1
{
    public partial class MainPage : UserControl, INotifyPropertyChanged
    {
        public int counter = 0;
       // ServiceReference1.Service1Client svcClient = new ServiceReference1.Service1Client();
        public MainPage()
        {
            
            InitializeComponent();
            //svcClient.GetRecordsCompleted += new EventHandler<ServiceReference1.GetRecordsCompletedEventArgs>(svcClient_GetRecordsCompleted);
            //Parameters = new List<object>() { 1, "AutoText" };
            this.radGridView1.KeyDown += new KeyEventHandler(playersGrid_KeyDown);
            this.Loaded += new RoutedEventHandler(MainPage_Loaded);
 
        }
        void playersGrid_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Tab)
            {
                Thread.Sleep(2000);
 
                var cell = ((TextBox)sender).ParentOfType<GridViewCell>();
                //var otherElement = cell.ChildrenOfType<YourControlType>().FirstOrDefault();
                MessageBox.Show("TAB key is pressed!");
            }
        }
        void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            LayoutRoot.DataContext = this;
 
        }
        //private List<object> _Parameters = null;
        //public List<object> Parameters
        //{
        //    get
        //    {
        //        if (_Parameters == null)
        //        {
        //            _Parameters = new List<object>();
        //        }
 
        //        return _Parameters;
        //    }
        //    set
        //    {
        //        _Parameters = value;
        //        this.NotifyChanged("Parameters");
        //    }
        //}
 
        public event PropertyChangedEventHandler PropertyChanged;
 
        //private bool IsServiceRunning = false;
        //private bool IsValidValue = false;
        //private bool enterkey = false;
        //private bool IsPopUp = false;
        //void svcClient_GetRecordsCompleted(object sender, ServiceReference1.GetRecordsCompletedEventArgs e)
        //{
        //    if (e.Result != null)
        //    {
        //        autoCompleteBox1.ItemsSource = e.Result;
        //        autoCompleteBox1.PopulateComplete();
        //    }
        //    if (autoCompleteBox1.SelectedItem != null)
        //    {
        //        IsValidValue = true;
        //        if (enterkey)
        //        {
        //            this.NextControlFocus();
        //        }
        //        IsServiceRunning = false;
        //    }
        //    else
        //    {
        //        IsPopUp = true;
        //        ChildWindow1 cw = new ChildWindow1();
        //        cw.Closed += new EventHandler(cw_Closed);
        //        cw.Show();
        //    }
        //    counter++;
        //    label2.Content = counter.ToString();
        //}
        
        //private void MaintainFocus()
        //{
        //    autoCompleteBox1.Focus();
        //}
      
     
        //public DependencyObject GetElementBasedOnName(DependencyObject startPoint, string elementName)
        //{
        //    DependencyObject returnValue = null;
        //    if (startPoint != null)
        //    {
        //        DependencyObject parent = VisualTreeHelper.GetParent(startPoint);
               
        //        if (parent != null)
        //        {
        //            FrameworkElement fe = parent as FrameworkElement;
 
        //            if (fe != null)
        //            {
        //                if (fe.Name == elementName)
        //                {
        //                    returnValue = fe;
        //                }
        //                else
        //                {
        //                    returnValue = fe.FindName(elementName) as DependencyObject;
        //                    if (returnValue == null)
        //                        returnValue = GetElementBasedOnName(fe, elementName);
        //                    else
        //                        return returnValue;
        //                }
        //            }
        //            else
        //            {
        //                returnValue = GetElementBasedOnName(fe, elementName);
        //            }
        //        }
        //    }
        //    return returnValue;
        //}
 
        //private void NextControlFocus()
        //{
        //    Control nextcontrol = this.GetElementBasedOnName(autoCompleteBox1 as DependencyObject, "textBox2") as Control;
        //    if (nextcontrol != null)
        //    {
        //        nextcontrol.Focus();
        //    }
        //}
 
        //void cw_Closed(object sender, EventArgs e)
        //{
        //    this.MaintainFocus();
        //    IsServiceRunning = false;
        //}
 
        //private string _name;
        //public string name1
        //{
        //    get { return _name; }
        //    set
        //    {
        //        if (value != null)
        //        {
        //            _name = value;
        //            NotifyChanged("name1");
        //        }
        //    }
        //}
 
 
        //private void autoCompleteBox1_Populating(object sender, PopulatingEventArgs e)
        //{
        //    //if (!this.IsServiceRunning && autoCompleteBox1.Text.Length > 0)
        //    //{
        //    //    IsServiceRunning = true;
        //    //    enterkey = false;
        //    //    e.Cancel = true;
        //    //    svcClient.GetRecordsAsync(1, autoCompleteBox1.Text);
        //    //}
        //}
 
        //private void autoCompleteBox1_GotFocus(object sender, RoutedEventArgs e)
        //{
        //    //if (!IsServiceRunning)
        //    //{
        //    //    IsValidValue = false;
        //    //    enterkey = false;
        //    //    IsPopUp = false;
        //    //}
        //}
 
 
        //private void autoCompleteBox1_LostFocus(object sender, RoutedEventArgs e)
        //{
        //    //if ((!IsValidValue || autoCompleteBox1.SelectedItem == null) && !IsPopUp)
        //    //{
        //    //    this.MaintainFocus();
        //    //    if (!IsServiceRunning && autoCompleteBox1.Text.Length > 0)
        //    //    {
 
        //    //        enterkey = true;
        //    //        IsServiceRunning = true;
        //    //        svcClient.GetRecordsAsync(1, autoCompleteBox1.Text);
        //    //    }
        //    //}
 
        //}
        public void NotifyChanged(string property)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(property));
            }
        }
 
        private ObservableCollection<Record> _DbStudents=null;
        public ObservableCollection<Record> DbStudents
        {
            get
            {
                if (_DbStudents == null)
                {
                    _DbStudents = this.GetStudentRecords();
                }
                return _DbStudents;
            }
            set
            {
                if (_DbStudents != value)
                {
                    _DbStudents = value;
                    this.NotifyChanged("DbStudents");
                }
            }
        }
        private object _SelectedRecord;
        public object SelectedRecord
        {
            get { return _SelectedRecord; }
            set
            {
                if (_SelectedRecord != value)
                {
                    _SelectedRecord = value;
                    this.NotifyChanged("SelectedRecord");
                }
            }
        }
 
        public ObservableCollection<Record> GetStudentRecords()
        {
            ObservableCollection<Record> r = new ObservableCollection<Record>();
          
            return new ObservableCollection<Record>() { new Record() { name = "vishwanath", address = "lucknow", age = 24 }, new Record() { name = "vi", address = "lucknow", age = 24 }, new Record() { name = "vis", address = "lucknow", age = 24 }, new Record() { name = "vishwa", address = "lucknow", age = 24 }, new Record() { name = "vish", address = "bangalore", age = 28 } };
        }
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            counter = 0;
            label2.Content = counter.ToString();
        }
 
        private void radGridView1_AddingNewDataItem(object sender, Telerik.Windows.Controls.GridView.GridViewAddingNewEventArgs e)
        {
            //radGridView1.CurrentColumn=radGridView1.Columns[0];
        }
 
        private void radGridView1_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            //var cell = ((TextBox)sender).Parent<GridViewCell>();
            var cell = radGridView1.ChildrenOfType<SilverlightApplication1.CustomAutoCompleteBox>().Where(c => c.Name.ToString() == "acbCustomerID1").FirstOrDefault();
 
            cell.Focus();
            //cell.IsInEditMode = true;
 
        }
        private void RadComboBox_GotFocus(object sender, RoutedEventArgs e)
        {
            (sender as RadComboBox).IsDropDownOpen = true;
        }
        
    }
 
    public class PassParameter
    {
        public PassParameter()
        {
        }
        private List<object> _Parameters = null;
        public List<object> Parameters
        {
            get
            {
                if (_Parameters == null)
                {
                    _Parameters = new List<object>() { 1, "AutoText" };
                }
 
                return _Parameters;
            }
            set
            {
                _Parameters = value;
 
            }
        }
    }
}



No answers yet. Maybe you can help?

Tags
GridView
Asked by
Amitkumar
Top achievements
Rank 1
Share this question
or