I have a DataGrid with an ItemSource set to an array of objects. The template column is set to a checkbox which is bound to the boolean IsChecked, and the rest of the columns are text columns set to strings. Despite setting SortMemberPaths and CanUserSort on the text columns, I'm still unable to sort any columns in the header. Any thoughts? Thanks in advance.
ItemSource Object
public class MyAllergy
{
public string Allergen { get; set; }
public string AllergenType { get; set; }
public string AllergyType { get; set; }
public string Reactions { get; set; }
public string TouchedWhen { get; set; }
public Boolean IsChecked { get; set; }
public Boolean IsEnabled { get; set; }
public string ApplicationSourceName { get; set; }
public string AllergyCategory { get; set; }
public string ConfidenceLevel { get; set; }
public PartialDate OnsetDate { get; set; }
public string Status { get; set; }
public string CreatedWhen { get; set; }
public string InformationSource { get; set; }
public string Text { get; set; }
public string ConfirmedBy { get; set; }
public long AllergyGUID { get; set; }
}
XAML
<DataGrid AlternatingRowBackground="#FFCDDAEB" AutoGenerateColumns="False" Background="White" Height="160" HorizontalAlignment="Stretch" Margin="18,355,6,0" Name="dgUnityAllergies" VerticalAlignment="Top" Width="704" CanUserAddRows="false" FontSize="12">
<DataGrid.Columns>
<DataGridTemplateColumn>
<DataGridTemplateColumn.HeaderTemplate>
<DataTemplate>
<CheckBox x:Name="all" Click="SelectAllCheckBox_Click" IsChecked="{Binding IsChecked}"/>
</DataTemplate>
</DataGridTemplateColumn.HeaderTemplate>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding IsChecked, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" IsEnabled="{Binding IsEnabled}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn CanUserSort="True" Header="Category" Width ="Auto" Binding="{Binding AllergyCategory}" IsReadOnly="True" SortMemberPath="AllergyCategory"/>
<DataGridTextColumn CanUserSort="True" Header="Type" Width ="Auto" Binding="{Binding AllergenType}" IsReadOnly="True" SortMemberPath="AllergenType"/>
<DataGridTextColumn CanUserSort="True" Header="Allergen" Width ="Auto" Binding="{Binding Allergen}" IsReadOnly="True" SortMemberPath="Allergen"/>
<DataGridTextColumn CanUserSort="True" Header="Campus" Width ="Auto" Binding="{Binding ApplicationSourceName}" IsReadOnly="True" SortMemberPath="ApplicationSourceName"/>
<DataGridTextColumn CanUserSort="True" Header="Reactions" Width ="*" Binding="{Binding Reactions}" IsReadOnly="True" SortMemberPath="Reactions"/>
<DataGridTextColumn CanUserSort="True" Header="Entered On" Width ="Auto" Binding="{Binding TouchedWhen}" IsReadOnly="True" SortMemberPath="TouchedWhen"/>
</DataGrid.Columns>
</DataGrid>
SortMemberPath="AllergyCategory
and etc. as DataGrid has such a feature by default – StepUpItemsSource
property in DataGrid? is it code-behind application? – StepUpAllergies
collection a property on your viewmodel and binding it to DataGrid.ItemsSource in the XAML. If you implementINotifyPropertyChanged
on your viewmodel and fire a notification correctly whenAllergies
changes, you can swap allergies collections by assigning different collections toAllergies
. Excuse me, I'm starting to feel itchy... – 15ee8f99-57ff-4f92-890c-b56153