也许,我认为,事情太简单了。我有一个数据网格,从后面的代码动态添加列。这个作品。然后我有整数值的列,我希望在列中看到右对齐,但是它失败了。我的代码是:
public List<List<int>> IntegerData { get; set; }................
DataGridTextColumn textC = new DataGridTextColumn();
textC.Header = ("Column");
textC.Binding = new Binding(string.Format("[{0}]", i));
textC.Binding.StringFormat = "00000";
结尾是这样的:
myDataGrid.Itemssource = IntegerData
textC.Binding.String.Format的问题是:(首先:看起来,stringFormat-LIne正确地重载了前一行)
格式"0"给出一个整数"1" "2",这是正确的。
格式"00000"给出"00001"00002"00003"这是正确的字符串格式,但不是,我想要的
但:
格式"######"也给出"1" "2" ..以此类推(左对齐,无前导空格)
根据我所期望(或希望)的格式规则,#####是描述的占位符,结果是"bbbb1", "bbbb2"右对齐。(b在这里是空白!)
所以Binding似乎可以正确处理"00000"格式,但不能处理"######"格式。有人对这个想法有什么想法或经验吗?(我在互联网上发现的所有其他绑定都要复杂得多)
您可以通过创建样式来解决这个问题,而不是在单元格中格式化文本。
首先,创建一个样式:Style style = new Style(typeof(DataGridCell));
style.Setters.Add(new Setter(HorizontalAlignmentProperty, HorizontalAlignment.Right));
将样式分配给列
textC.CellStyle = style;