如何在GridView(ListView)中对齐文本

问题描述 投票:0回答:1

我想以这种方式在GridViewColumn中声明ListView

<ListView ItemsSource="{Binding Sells}">
    <ListView.View>
        <GridView>
            <GridViewColumn Header="No. of order" Width="80"
                    DisplayMemberBinding="{Binding NoOfOrder, StringFormat=N0}"/>
            <GridViewColumn Header="Quantity" Width="100"
                    DisplayMemberBinding="{Binding Quantity, StringFormat=N0}"/>
            <GridViewColumn Header="Price" Width="100"
                    DisplayMemberBinding="{Binding Price, StringFormat=N2}"/>
        </GridView>
    </ListView.View>
</ListView>

但是问题是列标题以及单元格未按照我想要的方式对齐,我的应用程序中有几个ListView,我想要一些列标题以及单元格要居中对齐,其他要正确,我在此站点上找到的解决方案是执行以下操作:

<ListView ItemsSource="{Binding Buys}">
    <ListView.Resources>
        <Style TargetType="ListViewItem">
            <Setter Property="HorizontalContentAlignment" Value="Stretch" />
        </Style>
    </ListView.Resources>
    <ListView.View>
        <GridView>
            <GridViewColumn Width="80">
                <GridViewColumn.Header>
                    <GridViewColumnHeader Content="No. of order" HorizontalContentAlignment="Center"/>
                </GridViewColumn.Header>
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding NoOfOrder}" TextAlignment="Center"/>
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>

            <GridViewColumn Width="100">
                <GridViewColumn.Header>
                    <GridViewColumnHeader Content="Quantity" HorizontalContentAlignment="Right"/>
                </GridViewColumn.Header>
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Quantity, StringFormat=N0}" TextAlignment="Right"/>
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>

            <GridViewColumn Width="100">
                <GridViewColumn.Header>
                    <GridViewColumnHeader Content="Price" HorizontalContentAlignment="Right"/>
                </GridViewColumn.Header>
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Price, StringFormat=N2}" TextAlignment="Right"/>
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
        </GridView>
    </ListView.View>
</ListView>

[3列ListView太多了!我还有一些其他的具有7/8列,因此以这种方式重写所有这些列肯定很痛苦!有没有一种方法可以按照我在第一个示例中为ListView定义的方式声明这些Sells并具有一些模板和绑定机制,以便我可以用这种方式编写这些列:

<ListView ItemsSource="{Binding Sells}">
    <ListView.View>
        <GridView>
            <GridViewColumn Header="No. of order" Align="Center" Width="80"
                    DisplayMemberBinding="{Binding NoOfOrder, StringFormat=N0}"/>
            <GridViewColumn Header="Quantity" Align="Right" Width="100"
                    DisplayMemberBinding="{Binding Quantity, StringFormat=N0}"/>
            <GridViewColumn Header="Price" Align="Right" Width="100"
                    DisplayMemberBinding="{Binding Price, StringFormat=N2}"/>
        </GridView>
    </ListView.View>
</ListView>

或类似的东西?

c# wpf listview gridview
1个回答
1
投票
GridViewColumn

用法示例

© www.soinside.com 2019 - 2024. All rights reserved.