MoonKnight
MoonKnight

Reputation: 23833

Sort a Custom DataGridColumn in WPF

I have a custom DataGridColumn that was created to facilitate animation. Before I updated the column the column was sortable (provided by the framework), the mark-up is below

 <controls:ResourceDataGrid x:Name="resourceDataGrid" 
                            HorizontalAlignment="Stretch" 
                            VerticalAlignment="Stretch" 
                            AutoGenerateColumns="false" 
                            GridLinesVisibility="None"
                            RowHeaderWidth="0" 
                            CanUserAddRows="True" 
                            CanUserDeleteRows="True" 
                            ItemsSource="{Binding Path=Resources, 
                                                  Mode=TwoWay,
                                                  UpdateSourceTrigger=PropertyChanged, 
                                                  IsAsync=True}" 
                            dataAccess:DataGridTextSearch.SearchValue="{Binding ElementName=searchBox, 
                                                                                Path=Text, 
                                                                                UpdateSourceTrigger=PropertyChanged}" 
                            dataAccess:DataGridTextSearch.IsAnyTextMatch="False">
    <controls:ResourceDataGrid.Columns>
        <!--<DataGridTemplateColumn CellTemplate="{StaticResource readOnlyCellUpdatedStyle}"  IsReadOnly="True"/>-->
        <DataGridTextColumn Header="KeyIndex" Binding="{Binding KeyIndex}" IsReadOnly="True"/>
        <DataGridTextColumn Header="FileName" Binding="{Binding FileName}" IsReadOnly="True"/>
        <DataGridTextColumn Header="ResourceName" Binding="{Binding ResourceName}" IsReadOnly="False"/>
     <controls:CollectionTextColumn Collection="ResourceStringList" Visibility="Collapsed"/>
</controls:ResourceDataGrid.Columns>

the new column is shown above as commented and this is what prevents the column from being sortable. The code for the DataGridTemplateColumn is below

<DataTemplate x:Key="readOnlyCellUpdatedStyle">
    <TextBlock Text="{Binding KeyIndex, 
                              NotifyOnTargetUpdated=True, 
                              Mode=TwoWay}">
        <TextBlock.Style>
            <Style TargetType="TextBlock">
                <Setter Property="Background" Value="Transparent"/>
                <Style.Triggers>
                    <EventTrigger RoutedEvent="Binding.TargetUpdated">
                        <BeginStoryboard>
                            <Storyboard>
                                <ColorAnimation Storyboard.TargetProperty="Background.Color" Duration="0:0:1.5"
                                                From="Transparent" To="Orange" RepeatBehavior="1x" 
                                                AutoReverse="True">
                                    <ColorAnimation.EasingFunction>
                                        <ExponentialEase EasingMode="EaseInOut"/>
                                    </ColorAnimation.EasingFunction>
                                </ColorAnimation>
                            </Storyboard>
                        </BeginStoryboard>
                    </EventTrigger>
                </Style.Triggers>
            </Style>
        </TextBlock.Style>
    </TextBlock>
</DataTemplate>

How can I make this DataGridTemplateColumn sortable?

Thanks for your time.

Upvotes: 1

Views: 684

Answers (1)

sa_ddam213
sa_ddam213

Reputation: 43596

You will need to set the SortMemberPath on the DataGridTemplateColumn

Maybe try:

  <DataGridTemplateColumn SortMemberPath="KeyIndex" CellTemplate="{StaticResource readOnlyCellUpdatedStyle}"  IsReadOnly="True"/>

Upvotes: 2

Related Questions