MahApps / MahApps.Metro

A framework that allows developers to cobble together a better UI for their own WPF applications with minimal effort.
https://mahapps.com
MIT License
9.32k stars 2.45k forks source link

DataGrid "CanUserSortColumns" Not Working #1273

Closed amkuchta closed 10 years ago

amkuchta commented 10 years ago

I am using MA.M version 0.13.0.116-ALPHA, and am unable to sort any column other than a CheckboxColumn on any of my three DataGrids (one of which does not even have a CheckboxColumn. Below is the XAML for one of the DataGrids:

DataGrid XAML
<DataGrid x:Name="dgFiles"
                 Grid.Column="2"
                 Grid.ColumnSpan="3"
                 Margin="10,0,0,0"
                 Grid.RowSpan="8"
                 Grid.Row="1"
                 Height="403"
                 VerticalAlignment="Top"
                 BorderThickness="1"
                 ScrollViewer.VerticalScrollBarVisibility="Auto"
                 AllowDrop="True"
                 CanUserReorderColumns="True"
                 CanUserResizeColumns="True"
                 CanUserSortColumns="True"
                 AutoGenerateColumns="False"
                 ItemsSource="{Binding FileList}"
                 IsEnabled="{Binding IsEnabled}"
                 SelectionUnit="FullRow" 
                 BorderBrush="#FFCCCCCC" 
                 HorizontalGridLinesBrush="#FFCCCCCC" 
                 GridLinesVisibility="Horizontal">
 <DataGrid.Columns>
        <DataGridTemplateColumn Header="Filename"
                                                    Width="5*">
               <DataGridTemplateColumn.CellTemplate>
                     <DataTemplate>
                             <TextBlock Text="{Binding FileName}"
                                               TextTrimming="CharacterEllipsis"
                                               ToolTip="{Binding FileName}" />
                     </DataTemplate>
               </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
        <DataGridTemplateColumn Header="File Type"
                                                    Width="2.5*">
               <DataGridTemplateColumn.CellTemplate>
                      <DataTemplate>
                            <TextBlock Text="{Binding FileType}"
                                              TextTrimming="CharacterEllipsis"
                                              ToolTip="{Binding FileType}" />
                      </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
         </DataGridTemplateColumn>
         <DataGridTemplateColumn Header="Status"
                                                     Width="2.5*">
                 <DataGridTemplateColumn.CellTemplate>
                       <DataTemplate>
                              <TextBlock Text="{Binding Status}"
                                                TextTrimming="CharacterEllipsis"
                                                ToolTip="{Binding Status}" />
                         </DataTemplate>
                   </DataGridTemplateColumn.CellTemplate>
          </DataGridTemplateColumn>
    </DataGrid.Columns>
</DataGrid>

I think that the issue was introduced with this commit...

jimbobsmith commented 10 years ago

You'll need to set the 'SortMemberPath' property on any CellTemplateColumn you want sorting. For example, <DataGridTemplateColumn Header="Status" SortMemberPath="Status" ...

amkuchta commented 10 years ago

@jimbobsmith, this allows me to sort the DataGrid one time, but subsequent clicks on the other headers to not cause re-sorting. Any ideas?

jimbobsmith commented 10 years ago

You'll need to set that property on each Template column you want sorting on.

amkuchta commented 10 years ago

I did that: new XAML:

<DataGrid x:Name="dgFiles"
                 Grid.Column="2"
                 Grid.ColumnSpan="3"
                 Margin="10,0,0,0"
                 Grid.RowSpan="8"
                 Grid.Row="1"
                 Height="403"
                 VerticalAlignment="Top"
                 BorderThickness="1"
                 ScrollViewer.VerticalScrollBarVisibility="Auto"
                 AllowDrop="True"
                 CanUserReorderColumns="True"
                 CanUserResizeColumns="True"
                 CanUserSortColumns="True"
                 AutoGenerateColumns="False"
                 ItemsSource="{Binding FileList}"
                 IsEnabled="{Binding IsEnabled}"
                 SelectionUnit="FullRow" 
                 BorderBrush="#FFCCCCCC" 
                 HorizontalGridLinesBrush="#FFCCCCCC" 
                 GridLinesVisibility="Horizontal">
  <DataGrid.Columns>
    <DataGridTemplateColumn Header="Filename"
                                                 Width="5*"
                                                 SortMemberPath="Filename">
      <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
          <TextBlock Text="{Binding FileName}"
                             TextTrimming="CharacterEllipsis"
                             ToolTip="{Binding FileName}" />
        </DataTemplate>
      </DataGridTemplateColumn.CellTemplate>
    </DataGridTemplateColumn>
    <DataGridTemplateColumn Header="File Type"
                                                 Width="2.5*"
                                                 SortMemberPath="File Type">
      <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
          <TextBlock Text="{Binding FileType}"
                             TextTrimming="CharacterEllipsis"
                             ToolTip="{Binding FileType}" />
        </DataTemplate>
      </DataGridTemplateColumn.CellTemplate>
    </DataGridTemplateColumn>
    <DataGridTemplateColumn Header="Status"
                                                 Width="2.5*"
                                                 SortMemberPath="Status">
      <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
          <TextBlock Text="{Binding Status}"
                             TextTrimming="CharacterEllipsis"
                             ToolTip="{Binding Status}" />
        </DataTemplate>
      </DataGridTemplateColumn.CellTemplate>
    </DataGridTemplateColumn>
  </DataGrid.Columns>
</DataGrid>
jimbobsmith commented 10 years ago

Its typos in this case: 1) 'Filename' should be 'FileName' (capital N) to match the property name) 2) 'File Type' should be 'FileType' (no space) to match the property name

:)

amkuchta commented 10 years ago

Worked like a charm, thanks!

jimbobsmith commented 10 years ago

np