Formattazione del testo WPF in GridViewColumn
-
19-08-2019 - |
Domanda
Voglio applicare un formato (allinea il testo, formato per la valuta 0000.00) alle colonne in GridViewColumn.
<GridViewColumn TextBlock.TextAlignment="Center" Width="80" DisplayMemberBinding="{Binding XPath=Name}"/>
L'idea è la seguente: nelle colonne (GridViewColumn) il testo che il nostro potrebbe applicare a lui un formato (Aligner a sinistra, a destra, al centro, giustificare, ecc.)
Nel codice seguente possono vedere i diversi tentativi senza ottenere alcun risultato
Il codice è il seguente:
<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>
Soluzione
Ecco come ho fatto qualcosa di simile (formattare e allineare una colonna di valuta):
<GridViewColumn Header="Amount">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock TextAlignment="Right"
Text="{Binding Path=Amount, StringFormat='{}{0:C}'}" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
Ho anche aggiunto questo stile:
<Style TargetType="ListViewItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
</Style>
Se non si utilizza .NET 3.5 SP1, è necessario utilizzare un convertitore anziché StringFormat.
Altri suggerimenti
Per avere un formato stringa per la valuta, ad esempio, puoi utilizzare " StringFormat " che è stato introdotto sull'oggetto vincolante in .net3.5 sp1 credo.
Text="{Binding XPath=Code, StringFormat=0.000}"
Inoltre, devo dire che non capisco bene nemmeno la tua domanda.