如何使用绑定在代码中设置 DataGrid 列的格式

发布于 2024-11-26 06:55:35 字数 773 浏览 1 评论 0原文

我不知道如何将格式参数添加到以下 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 概念以保持问题更清晰)

I can't figure out how to add the format parameter to the following DataGrid column. I need to show the number with two decimal points.

I have a silverlight DataGrid that I'm adding columns to dynamically. I create the column and apply a dynamic binding (which I know works)

    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;
    }

Now I also need to format the value. In this case it is a float value being shown. How do I apply formatting to the binding? At this point all I want is the number and two decimal points to show, but I'd like it to be a little flexible and let me show a variable number of decimal points.

(Edit: Removed string IValueConverter concept to keep the question cleaner)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风蛊 2024-12-03 06:55:35

我讨厌回答我自己的问题,而且我认为我在如何使用值转换器添加潜在解决方案到我原来的问题中存在误导 - 对此感到抱歉。事实证明,解决方案很简单。您可以将格式字符串与绑定一起传递。

        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;
    }

I hate to answer my own question, and I think I was misleading in how I added a potential solution using a value converter to my original question - so sorry about that. The solution turned out to be simple. You pass the format string along with the binding.

        column.Binding.StringFormat = "0.00";

Here is the full solution

    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;
    }
红尘作伴 2024-12-03 06:55:35

您应该能够执行以下操作: newBinding.ConverterParameter = "formatString";

You should be able to do: newBinding.ConverterParameter = "formatString";

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文