Formatting WPF Text in a GridViewColumn

I want to apply a format (align text, format for currency 0000.00) to columns in a GridViewColumn.

 <GridViewColumn TextBlock.TextAlignment="Center" Width="80" DisplayMemberBinding="{Binding XPath=Name}"/>

The idea is as follows: in the columns (GridViewColumn) text in which we can apply a format to it (left, right, center alignments, alignment, etc.).

In the following code, they can see different attempts without getting the result.

The code is as follows:

 <Window x:Class="ListViewTest.Test0.ListViewTest"
   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
   Title="Empty ListView Grid" Height="216" Width="435" FlowDirection="LeftToRight" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Grid.IsSharedSizeScope="False">
    <Window.Resources>
        <XmlDataProvider x:Key="CustomersDS" Source="C:\data.xml"/>
        <Style x:Key="myHeaderStyle" TargetType="{x:Type GridViewColumnHeader}">
            <Setter Property="Visibility" Value="Collapsed" />
        </Style>
    </Window.Resources>



    <ListView Margin="0,0,0,50" ItemTemplate="{DynamicResource CustomerTemplate}" ItemsSource="{Binding Source={StaticResource CustomersDS}, XPath=/Customers/Customer}">
        <ListView.View>
            <!--ColumnHeaderContainerStyle="{StaticResource myHeaderStyle}"-->
            <GridView >
                <GridViewColumn Width="80" TextBlock.TextAlignment="Center">
                    <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock HorizontalAlignment="Center" Text="{Binding XPath=Code}"></TextBlock>
                    </DataTemplate>
                  </GridViewColumn.CellTemplate>
                </GridViewColumn>
                <GridViewColumn TextBlock.TextAlignment="Center" Width="80" DisplayMemberBinding="{Binding XPath=Name}"/>
                <GridViewColumn Width="120" TextBlock.TextAlignment="center" DisplayMemberBinding="{Binding XPath=Country}"/>
                <GridViewColumn Width="120" TextBlock.TextAlignment="center" DisplayMemberBinding="{Binding XPath=money}"/>
            </GridView>
        </ListView.View>
    </ListView>


</Window>

XML

     <Customers>
  <Customer>
 <Code>1234</Code>
 <Name>EPI</Name>
 <Country>Sesame Street</Country>
<money> 98.00</money>
  </Customer>
  <Customer>
 <Code>3234</Code>
 <Name>Paul</Name>
 <Country>United Kingdom</Country>
<money> 8.70</money>
  </Customer>
 <Customer>
 <Code>3344</Code>
 <Name>Juan</Name>
 <Country>Spain</Country>
<money> 785.5</money>
  </Customer>
 <Customer>
 <Code>4321</Code>
 <Name>Dodo</Name>
 <Country>Venezuela</Country>
<money> 150.02</money>
  </Customer>
</Customers>
+5
source share
2 answers

Here's how I did something similar (format and alignment of a currency column):

<GridViewColumn Header="Amount">
    <GridViewColumn.CellTemplate>
        <DataTemplate>
            <TextBlock TextAlignment="Right"
                       Text="{Binding Path=Amount, StringFormat='{}{0:C}'}" />
        </DataTemplate>
    </GridViewColumn.CellTemplate>
</GridViewColumn>

I also added this style:

<Style TargetType="ListViewItem">
    <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
</Style>

If you are not using .NET 3.5 SP1, you need to use a converter instead of StringFormat.

+17
source

, , "StringFormat", .net3.5 sp1, .

Text="{Binding XPath=Code, StringFormat=0.000}"

, , .

+2

All Articles