我找到了如何使用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);
}
答案 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非常困难。
答案 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 "\{0\}" inside a quot',
ElementName=userControl}"/>
</TextBlock>