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
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 >>
- Edit the name.
- Click anywhere else except save
- Click Save - the name is marked as change.
If however I do the following:
- Edit the name.
- 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);
}
}
}
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.
Didie
the Telerik team
Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>
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();
}
Didie
the Telerik team
Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>