Format values in a Datagrid

后端 未结 3 555
我寻月下人不归
我寻月下人不归 2020-12-15 10:00

Is there any way to format the values that are bound to a datagrid? For example I have the following:



        
相关标签:
3条回答
  • 2020-12-15 10:26

    Use the StringFormat property:

    <DataGridTextColumn Binding="{Binding Path=Date, StringFormat=d}" Header="Date" />
    <DataGridTextColumn Binding="{Binding Path=Amount, StringFormat=C}" Header="Amount" />
    
    • Standard Numeric Format Strings
    • Standard Date and Time Format Strings
    0 讨论(0)
  • 2020-12-15 10:33

    One easiest way. here in the code below use your language code as a value of ConverterCulture. you can find your language code here

    <DataGridTextColumn Binding="{Binding Profit, ConverterCulture='gu-IN' ,StringFormat=c}" Header="Profit" Width="*" MinWidth="80" FontWeight="Normal"/>
    

    the output will be in your local currency

    for anything other than currency find stringFormat specifier here

    0 讨论(0)
  • 2020-12-15 10:36

    If the stringformat is variable, the above solutions will not work.

    <DataGrid.Resources>
        <DataTemplate x:Key="YTemplate">
            <Label Content="{Binding Y, Mode=OneWay}" ContentStringFormat="{Binding StringFormat, ElementName=Parent_UC}"/>
        </DataTemplate>
    </DataGrid.Resources>
    <DataGrid.Columns>
        <DataGridTemplateColumn Header="Y" Width="*" CellTemplate="{StaticResource YTemplate}"/>
    </DataGrid.Columns>
    
    0 讨论(0)
提交回复
热议问题