我在MVVM中使用WPF。
我如何约束:
列表。
在我的ViewModel到WPF DataGrid的RowHeader?
发布于 2010-09-11 15:09:41
我从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();
}但是,我无法使行标题上的数字对齐。将HorizontalAlignment=“右”添加到TextBlock中是没有帮助的。我还试图将TextBlock包装到StackPanel中,并设置此属性,但它也不起作用。有什么想法吗?
发布于 2010-06-08 13:32:30
你可以采用这种方法http://www.codeproject.com/KB/WPF/MVVM_DataGrid.aspx?msg=3241301
基本上,只要T的对象实现ISequenceObject,您就会创建一个处理编号的列表。
如果您想转向另一个方向,您可以查看如何处理LoadingRow事件,只需将1添加到已知的DataColumn中即可。这不会打破MVVM的概念,因为编号列不是业务逻辑的一部分,而是表示的一部分。
要使编号的DataGrid可重用,还可以从DataGrid继承并创建自己的编号DataGrid。
https://stackoverflow.com/questions/2992710
复制相似问题