Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF ListView: Aligning text in selected columns

<ListView ItemsSource="{Binding MyData}">
  <ListView.View>
    <GridView>
      <GridViewColumn Header="col1" DisplayMemberBinding="{Binding Path=value1}">
        <GridViewColumn.CellTemplate>
          <DataTemplate>
            <TextBlock TextAlignment="Right" Text="{Binding Path=value1}"/>
          </DataTemplate>
        </GridViewColumn.CellTemplate>
      </GridViewColumn>
      <GridViewColumn Header="col2">
        <GridViewColumn.CellTemplate>
          <DataTemplate>
            <TextBlock TextAlignment="Center" Text="{Binding Path=value2}"/>
          </DataTemplate>
        </GridViewColumn.CellTemplate>
      </GridViewColumn>
      <GridViewColumn Header="col3" DisplayMemberBinding="{Binding Path=value3}"/>
    </GridView>
  </ListView.View>
<ListView ItemsSource="{Binding MyData}">

col1 is supposed to be right-aligned. (Not working)
col2 is supposed to be center-aligned. (Working)
col3 is supposed to be left-aligned. (Working)

Is there a reason DisplayMemberBinding is overriding CellTemplate? If so, is there a fix for this (while still using DisplayMemberBinding)?

Edit: I ended up implementing it like this:

<Window xmlns:util="clr-namespace:TestProject.Util">
  <Window.Resources>
    <Style TargetType="ListViewItem">
      <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
    </Style>
    <Style TargetType="GridViewColumnHeader">
      <Setter Property="HorizontalContentAlignment" Value="Left"/>
    </Style>
    <DataTemplate x:Key="value1Template">
      <TextBlock TextAlignment="Right" Text="{Binding Path=value1}"/>
    </DataTemplate>
    <DataTemplate x:Key="value2Template">
      <TextBlock TextAlignment="Right" Text="{Binding Path=value2}"/>
    </DataTemplate>
  </Window.Resources>
  <Grid>
    <ListView ItemsSource="{Binding MyData}" IsSynchronizedWithCurrentItem="True" util:GridViewSort.Command="{Binding SortCommand}">
      <ListView.View>
        <GridView>
          <GridViewColumn Header="col1" CellTemplate="{StaticResource value1Template}" util:GridViewSort.PropertyName="value1"/>
          <GridViewColumn Header="col2" CellTemplate="{StaticResource value2Template}" util:GridViewSort.PropertyName="value2"/>
        </GridView>
      </ListView.View>
    </ListView>
  </Grid>
</Window>

In the code behind:

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 > 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));
}
like image 410
snurre Avatar asked Jul 29 '10 07:07

snurre


2 Answers

DisplayMemberBinding has the highest priority. You can not use it combined with CellTemplate. See here in the remarks section.

If you want to right-or center-align the content, you must declare the CellTemplate with the binding (as you did) and remove the DisplayMemberBinding-attribute. If you also want to change the column header alignment, you must also set the GridViewColumn.Header-property.

like image 78
HCL Avatar answered Oct 01 '22 23:10

HCL


Just add the following after your Window tag:

  <Window.Resources>
    <Style TargetType="ListViewItem">
      <Setter Property="HorizontalContentAlignment" Value="Right" />
    </Style>
  </Window.Resources>
like image 24
Darwin Airola Avatar answered Oct 01 '22 21:10

Darwin Airola