GridViewColumn 中的 WPF 文本格式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/656296/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 20:08:48  来源:igfitidea点击:

WPF Text Formatting in GridViewColumn

wpfwpf-controls

提问by Ozplc

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

我想对 GridViewColumn 中的列应用格式(对齐文本,货币格式为 0000.00)。

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

The idea is the following one: In the columns (GridViewColumn) the text that our could apply a format to him (Aligners on the left, right, center, justify, etc.).

想法如下:在列 (GridViewColumn) 中,我们可以对其应用格式的文本(左侧对齐、右侧对齐、居中对齐、对齐等)。

In the following code they can see the different attempts without obtaining any 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

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>

回答by Ryan Versaw

Here is how I did something similar (format and align 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 aren't using .NET 3.5 SP1, you'll need to use a converter instead of the StringFormat.

如果您不使用 .NET 3.5 SP1,则需要使用转换器而不是 StringFormat。

回答by Tom Deleu

To have a stringformat for currency for example, you can use "StringFormat" which was introduced on the binding object in .net3.5 sp1 I think.

例如,要为货币设置字符串格式,您可以使用我认为在 .net3.5 sp1 中的绑定对象上引入的“StringFormat”。

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

Besides that, I must say I don't quite understand your question either.

除此之外,我必须说我也不太明白你的问题。