【问题标题】:format DataGridTextColumn from code-behind从代码隐藏格式化 DataGridTextColumn
【发布时间】:2014-05-10 16:39:09
【问题描述】:

也许,我认为,事情太简单了。 我有一个数据网格,从后面的代码中动态添加列。这行得通。 然后我有带有整数值的列,我希望在列中看到右对齐,但它失败了。我的代码是:

    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”格式,但不能正确处理“######”格式。 有人对此想法有任何想法或经验吗? (我在互联网上找到的所有其他绑定要复杂得多)

【问题讨论】:

  • 感谢您编辑我的问题。我是德国人,母语不是英语。尽管如此,我不仅喜欢提高软件开发,还喜欢提高我的英语

标签: c# wpf code-behind string.format datagridtextcolumn


【解决方案1】:

您可以通过创建样式而不是格式化单元格中的文本来解决您的问题。

首先,创建一个样式:

Style style = new Style(typeof(DataGridCell));
style.Setters.Add(new Setter(HorizontalAlignmentProperty, HorizontalAlignment.Right));

比将样式分配给列

textC.CellStyle = style;

【讨论】:

    猜你喜欢
    • 2012-10-16
    • 1970-01-01
    • 2013-01-24
    • 2016-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-02
    • 1970-01-01
    相关资源
    最近更新 更多