1

我不知道如何将格式参数添加到以下 DataGrid 列。我需要用两位小数显示数字。

我有一个silverlight DataGrid,我正在动态添加列。我创建列并应用动态绑定(我知道它有效)

    public static DataGridTextColumn CreateFloatColumn(int index, string fieldName, string header, string description)
    {
        DataGridTextColumn column = new DataGridTextColumn();
        column.Header = header;
        column.HeaderStyle = BuildColumnHeaderStyle(description);
        Binding newBinding = new Binding("floatValuesList[" + index + "]");
        column.Binding = newBinding;
        column.CellStyle = BuildCellStyle(fieldName, description);
        return column;
    }

现在我还需要格式化值。在这种情况下,显示的是一个浮点值。如何将格式应用于绑定?在这一点上,我想要的只是要显示的数字和两个小数点,但我希望它有点灵活,让我显示可变数量的小数点。

(编辑:删除字符串 IValueConverter 概念以保持问题更清晰)

4

2 回答 2

2

我讨厌回答我自己的问题,而且我认为我在如何使用价值转换器向我的原始问题添加潜在解决方案时具有误导性 - 对此感到抱歉。解决方案很简单。您将格式字符串与绑定一起传递。

        column.Binding.StringFormat = "0.00";

这是完整的解决方案

    public static DataGridTextColumn CreateFloatColumn(int index, string fieldName, string header, string description)
    {
        DataGridTextColumn column = new DataGridTextColumn();
        column.Header = header;
        column.HeaderStyle = BuildColumnHeaderStyle(description);
        column.Binding = new Binding("floatValuesList[" + index + "]");
        column.Binding.StringFormat = "0.00";
        column.CellStyle = BuildFloatCellStyle(fieldName, description);
        return column;
    }
于 2011-07-26T15:03:52.663 回答
0

你应该能够做到:newBinding.ConverterParameter = "formatString";

于 2011-07-26T04:55:58.317 回答