title: WPF ListView: Aligning text in selected columns
body: <pre><code>&lt;ListView ItemsSource="{Binding MyData}"&gt;
    &lt;ListView.View&gt;
      &lt;GridView&gt;
        &lt;GridViewColumn Header="col1" DisplayMemberBinding="{Binding Path=value1}"&gt;
          &lt;GridViewColumn.CellTemplate&gt;
            &lt;DataTemplate&gt;
              &lt;TextBlock TextAlignment="Right" Text="{Binding Path=value1}"/&gt;
            &lt;/DataTemplate&gt;
          &lt;/GridViewColumn.CellTemplate&gt;
        &lt;/GridViewColumn&gt;
        &lt;GridViewColumn Header="col2"&gt;
          &lt;GridViewColumn.CellTemplate&gt;
            &lt;DataTemplate&gt;
              &lt;TextBlock TextAlignment="Center" Text="{Binding Path=value2}"/&gt;
            &lt;/DataTemplate&gt;
          &lt;/GridViewColumn.CellTemplate&gt;
        &lt;/GridViewColumn&gt;
        &lt;GridViewColumn Header="col3" DisplayMemberBinding="{Binding Path=value3}"/&gt;
      &lt;/GridView&gt;
    &lt;/ListView.View&gt;
  &lt;ListView ItemsSource="{Binding MyData}"&gt;
  </code></pre>
  
  <p>col1 is supposed to be right-aligned. (Not working)<br>
  col2 is supposed to be center-aligned. (Working)<br>
  col3 is supposed to be left-aligned. (Working)  </p>
  
  <p>Is there a reason DisplayMemberBinding is overriding CellTemplate? If so, is there a fix for this (while still using DisplayMemberBinding)?</p>
  
  <p>Edit: I ended up implementing it like <strong>this</strong>:  </p>
  
  <pre><code>&lt;Window xmlns:util="clr-namespace:TestProject.Util"&gt;
    &lt;Window.Resources&gt;
      &lt;Style TargetType="ListViewItem"&gt;
        &lt;Setter Property="HorizontalContentAlignment" Value="Stretch"/&gt;
      &lt;/Style&gt;
      &lt;Style TargetType="GridViewColumnHeader"&gt;
        &lt;Setter Property="HorizontalContentAlignment" Value="Left"/&gt;
      &lt;/Style&gt;
      &lt;DataTemplate x:Key="value1Template"&gt;
        &lt;TextBlock TextAlignment="Right" Text="{Binding Path=value1}"/&gt;
      &lt;/DataTemplate&gt;
      &lt;DataTemplate x:Key="value2Template"&gt;
        &lt;TextBlock TextAlignment="Right" Text="{Binding Path=value2}"/&gt;
      &lt;/DataTemplate&gt;
    &lt;/Window.Resources&gt;
    &lt;Grid&gt;
      &lt;ListView ItemsSource="{Binding MyData}" IsSynchronizedWithCurrentItem="True" util:GridViewSort.Command="{Binding SortCommand}"&gt;
        &lt;ListView.View&gt;
          &lt;GridView&gt;
            &lt;GridViewColumn Header="col1" CellTemplate="{StaticResource value1Template}" util:GridViewSort.PropertyName="value1"/&gt;
            &lt;GridViewColumn Header="col2" CellTemplate="{StaticResource value2Template}" util:GridViewSort.PropertyName="value2"/&gt;
          &lt;/GridView&gt;
        &lt;/ListView.View&gt;
      &lt;/ListView&gt;
    &lt;/Grid&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p>In the code behind:</p>
  
  <pre><code>private RelayCommand sortCommand;
  public ICommand SortCommand { get { return sortCommand ?? (sortCommand = new RelayCommand(Sort)); } }
  private void Sort(object param)
  {
    var propertyName = param as string;
    var view = CollectionViewSource.GetDefaultView(MyData);
    var direction = ListSortDirection.Ascending;
    if (view.SortDescriptions.Count &gt; 0)
    {
      var currentSort = view.SortDescriptions[0];
      if (currentSort.PropertyName == propertyName)
        direction = currentSort.Direction == ListSortDirection.Ascending ? ListSortDirection.Descending : ListSortDirection.Ascending;
      view.SortDescriptions.Clear();
    }
    if (!string.IsNullOrEmpty(propertyName))
      view.SortDescriptions.Add(new SortDescription(propertyName, direction));
  }
  </code></pre>