列字符串格式

时间:2013-01-11 12:08:44

标签: c# wpf binding datagrid string-formatting

我找到了如何使用StringFormat中的XAML,但我需要以编程方式添加列,我无法找到解决方案。我使用的代码不起作用:

foreach (Fields field in fields)
{
    DataGridTextColumn column = new DataGridTextColumn();
    column.Header = field.name;
    column.Binding = new Binding(field.name) { Mode = BindingMode.TwoWay };
    if (field.group[0] == "<numbers>" && field.group.Count == 1)
        column.Binding.StringFormat = "{0:d}";
    dgwDataMain.Columns.Add(column);
}

3 个答案:

答案 0 :(得分:1)

这个怎么样?

column.Binding =
    new Binding(field.name)
    {
        Mode = BindingMode.TwoWay,
        UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
    };

UpdateSourceTrigger.PropertyChanged导致在每个键入的字符上更新绑定源。因此,每次都验证输入。

TextBox.Text属性(由DataGrid单元格使用)的默认UpdateSourceTrigger值为UpdateSourceTrigger.LostFocus

答案 1 :(得分:1)

然后你应该使用DataTable。 它的列可以是任何类型,添加新的DataGridColumn非常困难。

  1. 收集所有数据。
  2. 将正确类型的列添加到DataTable。
  3. 将所有数据添加到您的DataTable。
  4. 设置dataGrid.DataSource = DataTable;

答案 2 :(得分:0)

你应该试试你的Xaml代码

<TextBlock>
    <Run Text="Full date/time pattern (short time): "/>
    <Run Text="{Binding DateTime, StringFormat=f, ElementName=userControl}"/>
</TextBlock>
<TextBlock>
    <Run Text="Full date/time pattern (long time): "/>
    <Run Text="{Binding DateTime, StringFormat=F, ElementName=userControl}"/>
</TextBlock>
<TextBlock>
    <Run Text="Short date/time pattern (short time): "/>
    <Run Text="{Binding DateTime, StringFormat=g, ElementName=userControl}"/>
</TextBlock>
<TextBlock>
    <Run Text="Short date/time pattern (long time): "/>
    <Run Text="{Binding DateTime, StringFormat=G, ElementName=userControl}"/>
</TextBlock>

或者

<TextBlock>
    <Run Text="In Currency with zero decimal point: "/>
    <Run Text="{Binding Number, StringFormat=\{0:c0\}, ElementName=userControl}"/>
</TextBlock>
<TextBlock>
    <Run Text="In Currency with two decimal point: "/>
    <Run Text="{Binding Number, StringFormat=\{0:c2\}, ElementName=userControl}"/>
</TextBlock>

<TextBlock>
    <Run Text="Normal Number: "/>
    <Run Text="{Binding Number, ElementName=userControl}"/>
</TextBlock>
<TextBlock>
    <Run Text="Above number with 2 decimal point: "/>
    <Run Text="{Binding Number, StringFormat=\{0:n2\}, ElementName=userControl}"/>
</TextBlock>
<TextBlock>
    <Run Text="Above number with 4 decimal point: "/>
    <Run Text="{Binding Number, StringFormat=\{0:n4\}, ElementName=userControl}"/>
</TextBlock>
<TextBlock>
    <Run Text="Above number with 10 Zero place holder: "/>
    <Run Text="{Binding Number, StringFormat=\{0:0000000000\}, ElementName=userControl}"/>
</TextBlock>

<TextBlock>
    <Run Text="Normal string: "/>
    <Run Text="{Binding Text, ElementName=userControl}"/>
</TextBlock>
<TextBlock>
    <Run Text="String with atleast 15 characters length: "/>
    <Run Text="{Binding Text, StringFormat=\{0\,15\}, ElementName=userControl}"/>
</TextBlock>
<TextBlock>
    <Run Text="String with atleast 25 characters length: "/>
    <Run Text="{Binding Text, StringFormat=\{0\,25\}, ElementName=userControl}"/>
</TextBlock>
<TextBlock>
    <Run Text="Text with quote: "/>
    <Run Text="{Binding Text, StringFormat='The string &quot;\{0\}&quot; inside a quot',
                              ElementName=userControl}"/>
</TextBlock>