添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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; }
      <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>
                try to delete SortMemberPath="AllergyCategory and etc. as DataGrid has such a feature by default
– StepUp
                Mar 29, 2016 at 15:35
                @thunderchunky Your code works for me, with sorting, if I bind ItemsSource. Try making the Allergies collection a property on your viewmodel and binding it to DataGrid.ItemsSource in the XAML. If you implement INotifyPropertyChanged on your viewmodel and fire a notification correctly when Allergies changes, you can swap allergies collections by assigning different collections to Allergies. Excuse me, I'm starting to feel itchy...
– 15ee8f99-57ff-4f92-890c-b56153
                Mar 29, 2016 at 15:49
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.