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

editvalue not firing when clicking away

4 Answers 32 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Grant
Top achievements
Rank 1
Grant asked on 29 Jun 2012, 10:52 AM
I have the following issue:

I am binding a radgridview to a viewmodel and am currently not using the code behind and taking care of all the logic in the VM.

There is an "Apply" button bound to an IsDirty property being set once data changes in any of the bound items in the radgridview - this
is done via propertychanged events etc.

If I edit a value and click "apply" without clicking anywhere else, the edited value isn't commited to the bound model and apply doesn't
take the latest/most up to date value.

The closest thing I could find in forums is: http://www.telerik.com/community/forums/winforms/gridview/rowstate-question.aspx
but that didn't really help me.

Do you have any suggestions how I can get this to work, without breaking my design pattern.

4 Answers, 1 is accepted

Sort by
0
Dimitrina
Telerik team
answered on 29 Jun 2012, 12:10 PM
Hi,

May I ask you to provide more details on the problem you have. I am not sure how is the "Apply" button bound to the "IsDirty" property. Would you please clarify with some code snippets? How do you edit the value before click "Apply"?

Kind regards,
Didie
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

0
Grant
Top achievements
Rank 1
answered on 02 Jul 2012, 06:03 AM
If I have a xaml window ( this is an example ), bound to a viewmodel that notifies etc on name change I do the following:

  1. Edit the name.
  2. Click anywhere else except save
  3. Click Save - the name is marked as change.

If however I do the following:

  1. Edit the name.
  2. Click Save - the name is not marked as changed ( The edited changes are not commited )

Below is the mainwindow xaml, the viewmodel and the model code.


<Window x:Class="EditSample.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
        xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
        Title="MainWindow"
        Width="525"
        Height="350"
        >
    <Grid>
        <StackPanel Orientation="Vertical">
            <telerik:RadMenu Grid.Row="0"
                             Grid.ColumnSpan="2"
                             Margin="3"
                             VerticalAlignment="Top"
                             telerik:AnimationManager.AnimationSelector="{x:Null}"
                             >
                <telerik:RadMenuItem Margin="3"
                                     Click="RadMenuItem_Click"
                                     Header="Save"
                                     IsEnabled="{Binding HasChanges}"
                                     ToolTip="Save your model."
                                     />
            </telerik:RadMenu>

            <telerik:RadGridView Height="350"
                                 Margin="5"
                                 AutoGenerateColumns="False"
                                 CanUserFreezeColumns="False"
                                 CanUserInsertRows="False"
                                 CanUserReorderColumns="False"
                                 CanUserResizeColumns="False"
                                 CanUserSortColumns="False"
                                 EditTriggers="CellClick"
                                 IsFilteringAllowed="False"
                                 ItemsSource="{Binding TestModels}"
                                 RowDetailsVisibilityMode="Collapsed"
                                 RowIndicatorVisibility="Collapsed"
                                 SelectedItem="{Binding SelectedDataTemplate}"
                                 SelectionMode="Single"
                                 ShowGroupPanel="False"
                                 >
                <telerik:RadGridView.Columns>
                    <telerik:GridViewDataColumn Width="320"
                                                DataMemberBinding="{Binding Name}"
                                                Header="Template"
                                                IsVisible="True"
                                                >
                        <!--<telerik:GridViewDataColumn.CellEditTemplate>
                            <DataTemplate>
                                <TextBox Text="{Binding Name}" />
                            </DataTemplate>
                        </telerik:GridViewDataColumn.CellEditTemplate>-->
                    </telerik:GridViewDataColumn>
                </telerik:RadGridView.Columns>
            </telerik:RadGridView>
        </StackPanel>
    </Grid>
</Window>

/* start viewmodel
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows;
using EditSample.datamodels;

namespace EditSample.viewmodels
{
    public class MainWindowViewModel : INotifyPropertyChanged
    {
        private ObservableCollection<SomeModel> testModels;

        public ObservableCollection<SomeModel> TestModels
        {
            get { return testModels; }
            set
            {
                testModels = value;
                OnPropertyChanged(new PropertyChangedEventArgs("TestModels"));
            }
        }

        public MainWindowViewModel()
        {
            testModels = new ObservableCollection<SomeModel>();

            testModels.Add(new SomeModel()
                               {
                                   Id = Guid.NewGuid(),
                                   Name = "test name"
                               });
        }

        public void Save()
        {
            // alert the name of the name of the first model.
            MessageBox.Show(testModels.First().Name);
        }

        public event PropertyChangedEventHandler PropertyChanged;

        public void OnPropertyChanged(PropertyChangedEventArgs e)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null) handler(this, e);
        }
    }
}
end viewmode */

/// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = new MainWindowViewModel();
        }

        private void RadMenuItem_Click(object sender, Telerik.Windows.RadRoutedEventArgs e)
        {
            // assume the button is save for sake of this sample

            MainWindowViewModel mainWindowViewModel = (MainWindowViewModel) this.DataContext;
            mainWindowViewModel.Save();
        }
    }

/* model code below */
using System;
using System.ComponentModel;

namespace EditSample.datamodels
{
    public class SomeModel : INotifyPropertyChanged
    {
        private Guid id;
        private string name;

        public Guid Id
        {
            get { return id; }
            set
            {
                id = value;
                OnPropertyChanged(new PropertyChangedEventArgs("Id"));
            }
        }

        public string Name
        {
            get { return name; }
            set
            {
                name = value;
                OnPropertyChanged(new PropertyChangedEventArgs("Name"));
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        public void OnPropertyChanged(PropertyChangedEventArgs e)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null) handler(this, e);
        }
    }
}
0
Dimitrina
Telerik team
answered on 02 Jul 2012, 07:45 AM
Hello,

 Thank you for the sample code and the test project you have send us in your another thread. I will post the answer here so that the whole community could benefit from it:
The reason for this behavior is that the editing element (TextBox) does not loose its focus even when the Save element is pressed. What you can try is to focus another element of the Pressed event.

Regards,
Didie
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

0
Dimitrina
Telerik team
answered on 03 Jul 2012, 08:23 AM
Hello,

 As a follow up, I will update the thread with a better solution on how to change the focused element. You could directly benefit from the built-in commands of RadGridView: 

private void RadMenuItem_Click(object sender, Telerik.Windows.RadRoutedEventArgs e)
{
            var commitEditCommand = RadGridViewCommands.CommitCellEdit as RoutedUICommand;
            commitEditCommand.Execute(this.grid, null);
  
            MainWindowViewModel mainWindowViewModel = (MainWindowViewModel) this.DataContext;
            mainWindowViewModel.Save();
}


Regards,
Didie
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

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