listview中的xaml TextBlock未正确对齐

时间:2014-09-09 05:18:14

标签: c# wpf xaml

我想将列标题对齐左侧,将列中的详细信息对齐到右侧。 标题我说得对,但细节(textBlock)不想与右边对齐。请帮忙。这是我的代码

<ListView HorizontalAlignment="Stretch" 
 ItemsSource="{Binding Trans}" 
 Grid.Row="2"
 Grid.Column="0"
 Grid.ColumnSpan="5"
 ScrollViewer.VerticalScrollBarVisibility="Auto"
 MaxHeight="230">
 <!--Align column header to the left-->
 <ListView.Resources>
  <Style TargetType="{x:Type GridViewColumnHeader}">
   <Setter Property="HorizontalContentAlignment" Value="Left"/>
  </Style>
  <Style TargetType="{x:Type ListViewItem}">
   <Setter Property="HorizontalAlignment" Value="Stretch"/>
   </Style>
        </ListView.Resources>
        <ListView.View>
            <GridView ScrollViewer.HorizontalScrollBarVisibility="Auto">
                <GridViewColumn Header="Amount" >
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Foreground="Black" HorizontalAlignment="Right" Text="{Binding Path=Amount, StringFormat=N2}"/>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
            </GridView>
        </ListView.View>
    </ListView>

3 个答案:

答案 0 :(得分:1)

请将以下属性添加到文本块并检查

  

TextAlignment = “右”   到数据模板中的TextBlock节点

如果没有解决,请尝试添加

  

Setter Property =“Horizo​​ntalContentAlignment”Value =“right”to listview item Style

答案 1 :(得分:0)

这应该有效,

       <GridView ScrollViewer.HorizontalScrollBarVisibility="Auto">
            <GridViewColumn Header="Amount"  >
                <GridViewColumn.CellTemplate >
                    <DataTemplate >
                        <StackPanel Width="200"  >
                            <TextBlock Foreground="Black"  TextAlignment="Right" Text="{Binding Path=Amount, StringFormat=N2}"/>
                        </StackPanel>
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
        </GridView>

答案 2 :(得分:0)

尝试玩这样的东西:

<ListView Grid.IsSharedSizeScope="True">

    <ListView.Resources>
        <Style TargetType="{x:Type GridViewColumnHeader}">
           <Setter Property="HorizontalContentAlignment" Value="Left"/>         
        </Style>
        <Style TargetType="{x:Type ListViewItem}">
           <Setter Property="HorizontalAlignment" Value="Stretch"/> <!-- Redundent -->      
        </Style>
    </ListView.Resources>

    <ListView.View>
           <GridView>                               
            <GridViewColumn Header="Amount" >
            <GridViewColumn.HeaderTemplate>
                <DataTemplate>
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition SharedSizeGroup="A" />
                        </Grid.ColumnDefinitions>
                        <TextBlock Foreground="Black" Background="AliceBlue" Text="{Binding}"/>
                    </Grid>         
                </DataTemplate>
            </GridViewColumn.HeaderTemplate>

            <GridViewColumn.CellTemplate>
                <DataTemplate>
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition SharedSizeGroup="A" />
                        </Grid.ColumnDefinitions>

                        <TextBlock Foreground="Black" Background="AliceBlue" HorizontalAlignment="Right" Text="{Binding Path=Amount}"/>
                    </Grid>
                </DataTemplate>
            </GridViewColumn.CellTemplate>
            </GridViewColumn>
        </GridView>
    </ListView.View>
</ListView>