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

Updating ItemSource causes loss of focus

1 Answer 304 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Steve
Top achievements
Rank 1
Steve asked on 16 Jul 2012, 03:50 PM
I am using the RadGridView for a data entry screen (using MVVM), the last row is always empty and when the user starts to enter data into it a new row is added to the bottom on the list.  My issue is that once the itemSource is updated focus is taken out of the control and put into no mans land, making smooth data entry impossible.

The attached code is a cut down version of what i am doing and displays the same problem of lossing focus when the itemSource is updated.

XAML File
<Window x:Class="TelerikExample.MainWindow"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<DataTemplate x:Key="dtFirstName">
<TextBox Text="{Binding Path=FirstName, UpdateSourceTrigger=PropertyChanged, NotifyOnSourceUpdated=True}" />
</DataTemplate>
<DataTemplate x:Key="dtSecondName">
<TextBox Text="{Binding Path=SecondName, UpdateSourceTrigger=PropertyChanged, NotifyOnSourceUpdated=True}" />
</DataTemplate>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<telerik:RadGridView Grid.Row="1"
CanUserDeleteRows="False"
CanUserInsertRows="False"
CanUserSortColumns="False"
ShowGroupPanel="False"
AutoGeneratingColumn="RadGridView_AutoGeneratingColumn"
ItemsSource="{Binding Rows}"
RowIndicatorVisibility="Collapsed"
AlternateRowBackground="White"
AlternationCount="2"
Background="AliceBlue"
EditTriggers="None"
HorizontalGridLinesBrush="SlateGray"
SelectionMode="Single"
SelectionUnit="FullRow"
ShowColumnHeaders="True"
IsFilteringAllowed="False"
VerticalGridLinesBrush="Transparent">
</telerik:RadGridView>
</Grid>
</Window>


XMAL Code Behind
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
 
namespace TelerikExample
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private GridViewModel _viewModel;
 
        public MainWindow()
        {
            InitializeComponent();
 
            _viewModel = new GridViewModel();
            this.DataContext = _viewModel;
        }
 
        private void RadGridView_AutoGeneratingColumn(object sender, Telerik.Windows.Controls.GridViewAutoGeneratingColumnEventArgs e)
        {
            if (e.Column.UniqueName == "FirstName")
            {
                e.Column.CellTemplate = (DataTemplate)this.Resources["dtFirstName"];
                e.Column.Header = "Forename";
            }
            else if (e.Column.UniqueName == "SecondName")
            {
                e.Column.CellTemplate = (DataTemplate)this.Resources["dtSecondName"];
                e.Column.Header = "Surname";
            }
            else if (e.Column.UniqueName == "ViewModel")
            {
                e.Column.IsVisible = false;
            }
        }
    }
}

View model file

namespace TelerikExample
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.ComponentModel;
    using System.Dynamic;
    using System.Collections.ObjectModel;
 
    public class GridViewModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        private ObservableCollection<dynamic> _rows;
         
        /// <summary>
        ///
        /// </summary>
        /// <param name="name"></param>
        protected void OnPropertyChanged(string name)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(name));
            }
        }
 
        public GridViewModel()
        {
            _rows = new ObservableCollection<dynamic>();
 
            var row = new ExpandoObject() as IDictionary<string, Object>;
 
            row.Add("FirstName", string.Empty);
            row.Add("SecondName", string.Empty);
            row.Add("ViewModel", this);
 
            ((INotifyPropertyChanged)row).PropertyChanged += new PropertyChangedEventHandler(TelerikExample.PropertyChanged.Row_PropertyChanged);
 
            _rows.Add(row);
        }
 
        public void CheckForNewRow()
        {
 
            IDictionary<string, Object> lastRow = _rows.Last() as IDictionary<string, Object>;
 
            if (((string)lastRow["FirstName"]) != string.Empty || ((string)lastRow["SecondName"]) != string.Empty)
            {
                //Add new row               
                var row = new ExpandoObject() as IDictionary<string, Object>;
 
                row.Add("FirstName", string.Empty);
                row.Add("SecondName", string.Empty);
                row.Add("ViewModel", this);
 
                ((INotifyPropertyChanged)row).PropertyChanged += new PropertyChangedEventHandler(TelerikExample.PropertyChanged.Row_PropertyChanged);
 
                this.Rows.Add(row);
            }
        }
 
        public ObservableCollection<dynamic> Rows
        {
            get
            {
                return _rows;
            }
            set
            {
                if (_rows != value)
                {
                    _rows = value;
                    OnPropertyChanged("Rows");
                }
            }
 
        }
    }
}

Property changed event

namespace TelerikExample
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.ComponentModel;
    using System.Dynamic;
 
    public class PropertyChanged
    {
        public static void Row_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            object value;
            ((IDictionary<string, Object>)sender).TryGetValue("ViewModel", out value);
            GridViewModel vm = (GridViewModel)value;
 
            vm.CheckForNewRow();
        }
    }
}

So my question is is there a way in which when the itemSource is updated focus isn't lost from the textbox.

1 Answer, 1 is accepted

Sort by
0
Dimitrina
Telerik team
answered on 17 Jul 2012, 11:59 AM
Hi,

 Indeed, this would be the expected behaviour. You could set the GridView back in edit mode with this code:

this.myGridView.BeginEdit();
All the best,
Didie
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

Tags
GridView
Asked by
Steve
Top achievements
Rank 1
Answers by
Dimitrina
Telerik team
Share this question
or