如何使用绑定在代码中设置 DataGrid 列的格式
我不知道如何将格式参数添加到以下 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我讨厌回答我自己的问题,而且我认为我在如何使用值转换器添加潜在解决方案到我原来的问题中存在误导 - 对此感到抱歉。事实证明,解决方案很简单。您可以将格式字符串与绑定一起传递。
这是完整的解决方案
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.
Here is the full solution
您应该能够执行以下操作:
newBinding.ConverterParameter = "formatString";
You should be able to do:
newBinding.ConverterParameter = "formatString";