WPF ListView:对齐选定列中的文本

时间:2010-07-29 07:24:28

标签: c# wpf gridview listview

<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应该是右对齐的。 (不工作)
col2应该是居中对齐的。 (工作)
col3应该是左对齐的。 (工作)

DisplayMemberBinding是否有覆盖CellTemplate的原因?如果是这样,是否有解决方法(仍然使用DisplayMemberBinding)?

编辑:我最终实现了它,如 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>

在背后的代码中:

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));
}

2 个答案:

答案 0 :(得分:7)

DisplayMemberBinding具有最高优先级。您不能将它与CellTemplate结合使用。请参阅备注部分中的here

如果要对内容进行右对齐或居中对齐,则必须使用绑定声明CellTemplate(与您一样)并删除DisplayMemberBinding - 属性。如果您还想更改列标题对齐方式,还必须设置GridViewColumn.Header - 属性。

答案 1 :(得分:1)

只需在Window标记后添加以下内容:

  <Window.Resources>
    <Style TargetType="ListViewItem">
      <Setter Property="HorizontalContentAlignment" Value="Right" />
    </Style>
  </Window.Resources>