列字符串格式

本文关键字:格式 字符串 | 更新日期: 2023-09-27 17:52:12

我发现如何从XAML使用StringFormat,但我需要以编程方式添加列,我找不到解决方案。我使用的代码不工作:

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

列字符串格式

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

  1. 收集所有数据
  2. 为DataTable添加合适类型的列。
  3. 将所有数据添加到DataTable中。
  4. 设置数据表格。

这个怎么样?

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

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

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

你应该试试在你的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>