I have found a problem when using IsSynchronizedWithCurrentItem and removing items from an observable list from code-behind. Using the sample code below, if you:
This simple example seem to work fine without the IsSynchronizedWithCurrentItem flag, but my actual application still has similar problems without this flag set when deleting the item currently being edited, so I'm not sure if it's related.
Example Code:
MainWindow.xaml:
MainWindow.xaml.cs:
Louis
- Edit the ThirdItem Name field, then use the Delete button (while still in edit mode), ThirdItem remains on the screen but is successfully deleted from the code-behind. You can see this by sorting by any column, and the list gets redrawn correctly.
- Edit SecondItem Name field, then use the Delete button (while still in edit mode), the list now shows one Firstitem and two ThirdItems! Again, sorting redraws the list correctly.
This simple example seem to work fine without the IsSynchronizedWithCurrentItem flag, but my actual application still has similar problems without this flag set when deleting the item currently being edited, so I'm not sure if it's related.
Example Code:
MainWindow.xaml:
<
Window
x:Class
=
"SortedChangingList.MainWindow"
xmlns:telerik
=
"http://schemas.telerik.com/2008/xaml/presentation"
Title
=
"MainWindow"
Height
=
"350"
Width
=
"525"
>
<
Grid
>
<
telerik:RadGridView
IsSynchronizedWithCurrentItem
=
"True"
AutoGenerateColumns
=
"False"
ItemsSource
=
"{Binding ItemCollection}"
SelectionMode
=
"Single"
>
<
telerik:RadGridView.Columns
>
<
telerik:GridViewDataColumn
DataMemberBinding
=
"{Binding Path=Name}"
Header
=
"Name"
/>
<
telerik:GridViewDataColumn
DataMemberBinding
=
"{Binding Path=Comment}"
Header
=
"Comment"
/>
<
telerik:GridViewColumn
>
<
telerik:GridViewColumn.CellTemplate
>
<
DataTemplate
>
<
Button
Command="{Binding DataContext.DeleteCommand,
RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type telerik:RadGridView}}}"
CommandParameter
=
"{Binding }"
>Delete Me</
Button
>
</
DataTemplate
>
</
telerik:GridViewColumn.CellTemplate
>
</
telerik:GridViewColumn
>
</
telerik:RadGridView.Columns
>
</
telerik:RadGridView
>
</
Grid
>
</
Window
>
MainWindow.xaml.cs:
using
System;
using
System.Collections.ObjectModel;
using
System.Windows;
using
System.Windows.Controls;
using
System.Windows.Input;
namespace
SortedChangingList
{
public
class
Item
{
public
string
Name {
get
;
set
; }
public
string
Comment {
get
;
set
; }
}
public
partial
class
MainWindow : Window
{
private
RelayCommand _DeleteCommand;
public
ObservableCollection<Item> ItemCollection {
get
;
set
; }
public
ICommand DeleteCommand
{
get
{
return
_DeleteCommand ?? (_DeleteCommand =
new
RelayCommand(DeleteItem));
}
}
public
MainWindow()
{
ItemCollection =
new
ObservableCollection<Item>();
ItemCollection.Add(
new
Item() { Name =
"FirstItem"
});
ItemCollection.Add(
new
Item() { Name =
"SecondItem"
});
ItemCollection.Add(
new
Item() { Name =
"ThirdItem"
});
InitializeComponent();
DataContext =
this
;
}
private
void
DeleteItem(
object
obj)
{
Item item = obj
as
Item;
ItemCollection.Remove(item);
}
}
public
class
RelayCommand : ICommand
{
private
readonly
Action<
object
> _Execute;
public
RelayCommand(Action<
object
> execute)
{
_Execute = execute;
}
public
bool
CanExecute(
object
parameter)
{
return
true
;
}
public
event
EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
public
void
Execute(
object
parameter)
{
_Execute(parameter);
}
}
}
Louis