0

我有下面的xaml代码。我正在尝试将图像和textblock右侧对齐。第一个textblock显示正确,我只想要最右边的image第二个。我怎样才能正确对齐?textblockdock

<Border Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="3" BorderThickness="0" Padding="16,2"
        BorderBrush="Transparent" Background="{StaticResource AccentBrush}">
    <DockPanel LastChildFill="True">                
            <ItemsControl ItemsSource="{Binding Lists}">
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <TextBlock Margin="0,5" FontSize="24" FontFamily="Sageo UI Light" TextAlignment="Left" TextWrapping="NoWrap"
                           TextTrimming="WordEllipsis" Foreground="{StaticResource MainBrush}" HorizontalAlignment="Stretch" Text="{Binding Title}" />
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>
          <StackPanel Orientation="Horizontal" >
            <Image HorizontalAlignment="Right" Source="/Images/image.png"/>
            <TextBlock DockPanel.Dock="Right" Margin="16,0,0,0" Text="{Binding ElementName=TreeView, Path=SelectedItem.Name, StringFormat='Name: {0}'}"
                   FontFamily="Segoe UI Light" FontSize="14" TextAlignment="Right" TextWrapping="NoWrap" TextTrimming="WordEllipsis"
                   Foreground="{StaticResource MainBrush}" HorizontalAlignment="Stretch" VerticalAlignment="Center" />               
        </StackPanel>                
    </DockPanel>
</Border>
4

1 回答 1

1

a 的内部大小StackPanel在其方向上是无限的,Orientation并且总是从顶部/左侧堆叠项目。这两个属性的组合意味着您不能使用 aStackPanel来获得您正在寻找的正确对齐方式。相反,您可以使用另一个DockPanel,或者直接将所有内容放在外部DockPanel。我无法从您的代码中看出您想要正确停靠项目的顺序,但这至少应该让您开始:

<Border Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="3" BorderThickness="0" Padding="16,2"
        BorderBrush="Transparent" Background="{StaticResource AccentBrush}">
    <DockPanel LastChildFill="False">                
            <ItemsControl ItemsSource="{Binding Lists}">
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <TextBlock Margin="0,5" FontSize="24" FontFamily="Sageo UI Light" TextAlignment="Left" TextWrapping="NoWrap"
                           TextTrimming="WordEllipsis" Foreground="{StaticResource MainBrush}" HorizontalAlignment="Stretch" Text="{Binding Title}" />
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>
          <DockPanel DockPanel.Dock="Right">
            <Image Source="/Images/image.png"/>
            <TextBlock Margin="16,0,0,0" Text="{Binding ElementName=TreeView, Path=SelectedItem.Name, StringFormat='Name: {0}'}"
                   FontFamily="Segoe UI Light" FontSize="14" TextAlignment="Right" TextWrapping="NoWrap" TextTrimming="WordEllipsis"
                   Foreground="{StaticResource MainBrush}" HorizontalAlignment="Stretch" VerticalAlignment="Center" />               
        </DockPanel>                
    </DockPanel>
</Border>
于 2013-05-16T20:36:34.117 回答