2

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

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

3 回答 3

1

这个怎么样?

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

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

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

于 2013-01-11T12:11:54.423 回答
1

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

  1. 收集所有数据。
  2. 将正确类型的列添加到 DataTable。
  3. 将所有数据添加到您的 DataTable。
  4. 设置 dataGrid.DataSource = DataTable;
于 2013-01-11T12:54:13.543 回答
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>
于 2013-01-11T12:18:28.400 回答