在WPF DataGrid RowHeader中显示运行编号

msf*_*boy 2 wpf datagrid row header mvvm

我正在使用WPF与MVVM.

我怎么绑定:

  • 一个数字列表
  • 或具有属性编号的clr对象列表
  • 或字符串列表

在我的ViewModel中WPF DataGrid的RowHeader?

mil*_*liu 8

我从http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/7d0cbdf2-dea3-4dd4-a570-08aa6c78d911找到了解决此问题的优雅解决方案.这是概述:

<tk:DataGrid x:Name="dg" LoadingRow="dg_LoadingRow" ItemsSource="{Binding}">
    <tk:DataGrid.RowHeaderTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type tk:DataGridRow}}, Path=Header}"></TextBlock>
        </DataTemplate>
    </tk:DataGrid.RowHeaderTemplate>
</tk:DataGrid>

private void dg_LoadingRow(object sender, Microsoft.Windows.Controls.DataGridRowEventArgs e)
{
    e.Row.Header = (e.Row.GetIndex() + 1).ToString();
}
Run Code Online (Sandbox Code Playgroud)

但是,我无法使行标题上的数字右对齐.将Horizo​​ntalAlignment ="Right"添加到TextBlock没有帮助.我还尝试将TextBlock包装在StackPanel中,并设置此属性,但它也不起作用.任何的想法?