Hi,
I have a data grid with UserSortMode set to "Single".
If i use my columns as DataGridTextColumn every thing works as expected.
If I modify my column as DataGridTempateColumn and Specify SortDescriptor as "DelegateSortDescriptor", Data is sorted as expected, but the sort icon in column header remains as is.
Please find the code snippet below.
Steps to regenerate
- Open the application
- Sort "Id" Column (Ascending)
- Immediately sort "Name" column
Note that Name column is sorted, but notice that the sort icon on "Id" column is retained, which is not expected.
Please have a look at the issue and point me if i am missing any thing.
Code Snippet
>>>>>>>>>>>>
MainPage.xaml
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:telerik="clr-namespace:Telerik.XamarinForms.DataGrid;assembly=Telerik.XamarinForms.DataGrid"
xmlns:local="clr-namespace:GridSorting"
x:Class="GridSorting.Views.MainPage"
Title="{Binding Title}">
<StackLayout>
<telerik:RadDataGrid ItemsSource="{Binding Items}" AutoGenerateColumns="False" SelectionMode="Single" UserSortMode="Single">
<telerik:RadDataGrid.SelectionStyle>
<telerik:DataGridBorderStyle BackgroundColor="LightGoldenrodYellow" BorderColor="DarkBlue"/>
</telerik:RadDataGrid.SelectionStyle>
<telerik:RadDataGrid.Columns>
<!--<telerik:DataGridNumericalColumn PropertyName="Id" HeaderText="ID"/>-->
<telerik:DataGridTemplateColumn HeaderText="ID">
<telerik:DataGridTemplateColumn.SortDescriptor>
<local:MySortDescriptor PropertyName="Id" />
</telerik:DataGridTemplateColumn.SortDescriptor>
<telerik:DataGridTemplateColumn.CellContentTemplate>
<DataTemplate>
<Grid Padding="10">
<Label Text="{Binding Id}"/>
</Grid>
</DataTemplate>
</telerik:DataGridTemplateColumn.CellContentTemplate>
</telerik:DataGridTemplateColumn>
<!--<telerik:DataGridTextColumn PropertyName="Name" HeaderText="Name"/>-->
<telerik:DataGridTemplateColumn HeaderText="Name">
<telerik:DataGridTemplateColumn.SortDescriptor>
<local:MySortDescriptor PropertyName="Name" />
</telerik:DataGridTemplateColumn.SortDescriptor>
<telerik:DataGridTemplateColumn.CellContentTemplate>
<DataTemplate>
<Grid Padding="10">
<Label Text="{Binding Name}"/>
</Grid>
</DataTemplate>
</telerik:DataGridTemplateColumn.CellContentTemplate>
</telerik:DataGridTemplateColumn>
<!--<telerik:DataGridTextColumn PropertyName="City" HeaderText="City"/>-->
<telerik:DataGridTemplateColumn HeaderText="City">
<telerik:DataGridTemplateColumn.SortDescriptor>
<local:MySortDescriptor PropertyName="City" />
</telerik:DataGridTemplateColumn.SortDescriptor>
<telerik:DataGridTemplateColumn.CellContentTemplate>
<DataTemplate>
<Grid Padding="10">
<Label Text="{Binding City}"/>
</Grid>
</DataTemplate>
</telerik:DataGridTemplateColumn.CellContentTemplate>
</telerik:DataGridTemplateColumn>
</telerik:RadDataGrid.Columns>
</telerik:RadDataGrid>
</StackLayout>
</ContentPage>
namespace GridSorting
{
public class MySortDescriptor : DelegateSortDescriptor
{
public MySortDescriptor()
{
KeyLookup = new MyKeyLookUp(this);
Comparer = new MyComparer();
}
public string PropertyName { get; set; }
}
public class MyKeyLookUp : IKeyLookup
{
private MySortDescriptor mySortDescriptor;
public MyKeyLookUp(MySortDescriptor mySortDescriptor)
{
this.mySortDescriptor = mySortDescriptor;
}
public object GetKey(object instance)
{
var objectAccessor = ObjectAccessor.Create(instance);
return objectAccessor[mySortDescriptor.PropertyName];
}
}
public class MyComparer : IComparer
{
public int Compare(object x, object y)
{
return string.Compare(x.ToString(), y.ToString());
}
}
}
namespace GridSorting.ViewModels
{
public class MainPageViewModel : ViewModelBase
{
private ObservableCollection<MainPageModel> _items;
public MainPageViewModel(INavigationService navigationService)
: base(navigationService)
{
Title = "Main Page";
Items = new ObservableCollection<MainPageModel>(new List<MainPageModel>()
{
new MainPageModel() { Id = 1, Name = "Name 1", City = "City 1" },
new MainPageModel() { Id = 2, Name = "Name 2", City = "City 2" },
new MainPageModel() { Id = 3, Name = "Name 3", City = "City 3" },
new MainPageModel() { Id = 4, Name = "Name 4", City = "City 4" },
new MainPageModel() { Id = 5, Name = "Name 5", City = "City 5" },
new MainPageModel() { Id = 6, Name = "Name 6", City = "City 6" },
new MainPageModel() { Id = 7, Name = "Name 7", City = "City 7" },
new MainPageModel() { Id = 8, Name = "Name 8", City = "City 8" },
new MainPageModel() { Id = 9, Name = "Name 9", City = "City 9" },
new MainPageModel() { Id = 10, Name = "Name 10", City = "City 10" },
});
}
public ObservableCollection<MainPageModel> Items
{
get { return _items; }
set { SetProperty(ref _items, value); }
}
}
public class MainPageModel
{
public int Id { get; set; }
public string Name { get; set; }
public string City { get; set; }
}
}
<<<<<<<<<<<<
Thank you.