WPF Xaml - 将图像对齐

时间:2017-05-10 11:51:29

标签: wpf xaml

刚开始使用xaml。

我有以下内容:

<StackPanel x:Name="Compare"
                            Orientation="Horizontal">
                    <Label Content="Path to Access DB:"
                           HorizontalAlignment="Left"
                           Margin="3"
                           Width="120"
                           VerticalAlignment="Center" />
                    <TextBox x:Name="txtPathToAccess"
                             HorizontalAlignment="Left"
                             Height="23"
                             Margin="3"
                             Text="{Binding PathToAccessDb}"
                             VerticalAlignment="Center"
                             Width="800" />
                    <Button x:Name="btnFileSearch"
                            Content="..."
                            Width="20"
                            Height="20"
                            Command="{Binding AccessFileSearchCommand}" />
                    <Button x:Name="btnComparePiToAccess"
                            Margin="10,0,0,0"
                            Content="Compare Pi to Access"
                            Width="150"
                            Height="20"
                            Command="{Binding CompareCommandPiToAccess}" />
                    <Image Source="/Images/drax_logo.jpg" 
                           Width="100" />
                </StackPanel>

如何让图像与堆叠面板的右边缘对齐?我想要屏幕左侧的标签和按钮(行)和右侧的图像。

1 个答案:

答案 0 :(得分:3)

您无法使用stackpanel执行此操作。相反,创建一个包含2列的网格,并将图像放在第二列中,并将HorizontalAlignment设置为右侧。

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto"></ColumnDefinition>
        <ColumnDefinition></ColumnDefinition>
    </Grid.ColumnDefinitions>

    <StackPanel Grid.Column="0" x:Name="Compare"
                        Orientation="Horizontal">
        <Label Content="Path to Access DB:"
                       HorizontalAlignment="Left"
                       Margin="3"
                       Width="120"
                       VerticalAlignment="Center" />
        <TextBox x:Name="txtPathToAccess"
                         HorizontalAlignment="Left"
                         Height="23"
                         Margin="3"
                         Text="{Binding PathToAccessDb}"
                         VerticalAlignment="Center"
                         Width="800" />
        <Button x:Name="btnFileSearch"
                        Content="..."
                        Width="20"
                        Height="20"
                        Command="{Binding AccessFileSearchCommand}" />
        <Button x:Name="btnComparePiToAccess"
                        Margin="10,0,0,0"
                        Content="Compare Pi to Access"
                        Width="150"
                        Height="20"
                        Command="{Binding CompareCommandPiToAccess}" />

    </StackPanel>

    <Image Grid.Column="1" 
           HorizontalAlignment="Right"
           Source="/Images/drax_logo.jpg" 
           Width="100" />
</Grid>

希望这有帮助。