代码之家  ›  专栏  ›  技术社区  ›  Siegfried.V

WPF-包装上的水平对齐

  •  -2
  • Siegfried.V  · 技术社区  · 7 年前

    <GridViewColumn Header="{x:Static p:Resources.Priorite}" Width="70">
      <GridViewColumn.CellTemplate>
        <DataTemplate>
          <WrapPanel HorizontalAlignment="Center">
            <TextBlock HorizontalAlignment="Center" TextAlignment="Center">
              <TextBlock Text="{Binding PrioriteMin}"/>
              <TextBlock Text=" - " />
              <TextBlock Text="{Binding PrioriteMax}"/>
            </TextBlock>
          </WrapPanel>
        </DataTemplate>
      </GridViewColumn.CellTemplate>
    </GridViewColumn>
    
    3 回复  |  直到 7 年前
        1
  •  1
  •   nosale    7 年前

    你的问题是当 FrameworkElement 它显示创建时的单元格内容,并从相应的 ListViewItem . 这导致了 HorizontalAlignment.Left .

    https://docs.microsoft.com/de-de/dotnet/framework/wpf/advanced/alignment-margins-and-padding-overview

    因此,要实现你想要的,你必须设定 HorizontalContentAlignment Stretch 在你的 .

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

    另见 In WPF Stretch a control to fill a ListView Column

        2
  •  -2
  •   Echo    7 年前

    <WrapPanel>
         <WrapPanel.Resources>
                <Style TargetType="TextBlock">
                    <Setter Property="HorizontalAlignment" Value="Center"/>
                </Style>
         </WrapPanel.Resources>
         <TextBlock>MyText</TextBlock>
         <TextBlock>MyText2</TextBlock>
    <WrapPanel>
    

    我认为上述要求适用于你的情况,尽管我可能误解了要求。

        3
  •  -3
  •   tombobadil    7 年前

    <WrapPanel HorizontalAlignment="Center">
    

    这样做的诀窍,你甚至不需要

    HorizontalAlignment="Center" TextAlignment="Center"