title: How do I databind DataGridColumn.SortDirection in WPF, using XAML?
body: <p>This is related to <a href="https://stackoverflow.com/questions/1312613/databinding-the-datagrid-column-header-in-code">Databinding the DataGrid column header in code</a> but it asks the opposite question: &quot;How do I do it in XAML?&quot;, not &quot;How do I do it in code?&quot;</p>
  <p>I've constructed this MainWindow.xaml</p>
  <pre><code>&lt;Window x:Class=&quot;MainWindow&quot;
          xmlns=&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;
          xmlns:x=&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot;
          Title=&quot;MainWindow&quot; Height=&quot;450&quot; Width=&quot;800&quot;
          DataContext=&quot;{Binding RelativeSource={RelativeSource Self}}&quot;&gt;
      &lt;Grid&gt;
          &lt;DataGrid AutoGenerateColumns=&quot;False&quot; CanUserSortColumns=&quot;True&quot; Sorting=&quot;DataGrid_Sorting&quot; Margin=&quot;5&quot; ItemsSource=&quot;{Binding ItemsSource}&quot;&gt;
              &lt;DataGrid.Columns&gt;
                  &lt;DataGridTextColumn Binding=&quot;{Binding Value}&quot; SortDirection=&quot;{Binding SortDirection, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}, Mode=TwoWay}&quot; Header=&quot;Click Me&quot; x:Name=&quot;column&quot;/&gt;
              &lt;/DataGrid.Columns&gt;
          &lt;/DataGrid&gt;
      &lt;/Grid&gt;
  &lt;/Window&gt;
  </code></pre>
  <p>and this MainWindow.xaml.cs</p>
  <pre><code>using System.Collections.Generic;
  using System.ComponentModel;
  using System.Linq;
  using System.Windows;
  
  public partial class MainWindow : Window
  {
      private ListSortDirection? _sortDirection;
      public ListSortDirection? SortDirection
      {
          get =&gt; _sortDirection;
          set
          {
              // I expect this message box to appear, but it doesn't.
              MessageBox.Show($&quot;Setting sort direction to {value?.ToString() ?? &quot;null&quot;}&quot;);
              _sortDirection = value;
          }
      }
  
      public IEnumerable&lt;object&gt; ItemsSource { get; } = new[] { 1, 2, 3, 4, 5 }.Select(x =&gt; new { Value = x }).ToList();
  
      public MainWindow() =&gt; InitializeComponent();
  
      private void DataGrid_Sorting(object sender, System.Windows.Controls.DataGridSortingEventArgs e)
          =&gt; MessageBox.Show($&quot;Sorting grid.\nColumn sort direction was {column.SortDirection?.ToString() ?? &quot;null&quot;}.&quot;);
  }
  </code></pre>
  <p>The binding works as expected for <code>DataGrid.ItemsSource</code>, but no binding I've constructed works for <code>DataGridColumn.SortDirection</code>. When clicking the column header (&quot;Click Me&quot;), I expect to see both message boxes, but only one appears.</p>
  <hr />
  <p>I can put <code>BindingOperations.SetBinding(column, DataGridColumn.SortDirectionProperty, new Binding(nameof(SortDirection)) { Source = this, Mode = BindingMode.TwoWay });</code> in the constructor to set up the binding as expected, but I've been brought up to believe that bindings should be constructed in XAML, not in code.</p>