为什么StackPanel不会在Silverlight中左侧放置文本块而在右侧放置按钮?

时间:2009-06-15 22:09:02

标签: silverlight xaml stackpanel

好的,我放弃了:我需要更改下面的 StackPanel ,以便它放置:

  • 表格最左侧的文字
  • 表格最右侧的
  • 按钮

alt text http://tanguay.info/web/external/stackPanelLeftRight.png

<UserControl x:Class="TestData333.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Width="400" Height="300">
    <Grid x:Name="LayoutRoot" Background="White">
        <Border CornerRadius="10" Background="Yellow" Padding="20">
            <StackPanel VerticalAlignment="Top" HorizontalAlignment="Left">
                <ScrollViewer Background="Beige" 
                              Height="230"
                              Width="360">
                    <StackPanel>
                        <TextBlock x:Name="TheContent" 
                           Foreground="Navy"
                           FontSize="14"
                           TextWrapping="Wrap"/>
                    </StackPanel>
                </ScrollViewer>

                <StackPanel Orientation="Horizontal">
                    <TextBlock x:Name="ProgressIndicator" Text="Ready..."
                               HorizontalAlignment="Left"/>
                    <Button Content="Load Data"
                        Width="100"
                        HorizontalAlignment="Right"
                        Click="Button_Load"
                        Margin="0 5 0 0"/>
                </StackPanel>

            </StackPanel>
        </Border>
    </Grid>
</UserControl>

解答:

已安装DockPanel的已下载Silverlight 3 toolkit,已引用System.Windows.Controls,然后是XAML:

<UserControl x:Class="TestData333.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:toolkit="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls"
    Width="400" Height="300">
    <Grid x:Name="LayoutRoot" Background="White">
        <Border CornerRadius="10" Background="Yellow" Padding="20">
            <StackPanel VerticalAlignment="Top" HorizontalAlignment="Left">
                <ScrollViewer Background="Beige" 
                              Height="230"
                              Width="360">
                    <StackPanel>
                        <TextBlock x:Name="TheContent" 
                           Foreground="Navy"
                           FontSize="14"
                           TextWrapping="Wrap"/>
                    </StackPanel>
                </ScrollViewer>

                <toolkit:DockPanel Margin="0 5 0 0">
                    <TextBlock toolkit:DockPanel.Dock="Left" x:Name="ProgressIndicator" Text="Ready..."
                               FontSize="12"
                               HorizontalAlignment="Left"/>
                    <Button toolkit:DockPanel.Dock="Right" Content="Load Data"
                        Width="100"
                        HorizontalAlignment="Right"
                        Click="Button_Load"/>
                </toolkit:DockPanel>

            </StackPanel>
        </Border>
    </Grid>
</UserControl>

alt text http://tanguay.info/web/external/silverlightDockPanel.png

4 个答案:

答案 0 :(得分:11)

您可以使用工具包中的dockpanel或使用2列的网格。并使第二列的内容右对齐

答案 1 :(得分:7)

您的意思是您希望按钮与表单右侧对齐吗?如果是这样,StackPanel将不会这样做。这是为了“横向或纵向堆叠”。

我建议您尝试DockPanel

<DockPanel>
    <TextBlock x:Name="ProgressIndicator" 
               DockPanel.Dock="Left"
               Text="Ready..." />
    <Button DockPanel.Dock="Right"
            Content="Load Data"
            Width="100"
            Click="Button_Load"
            Margin="0,5,0,0" />
</DockPanel>

答案 2 :(得分:1)

我认为马特的态度是最好的。但有两种选择是使用网格并将内容左右对齐,或者只给按钮一个很大的余量。

答案 3 :(得分:1)

参考应该是:

xmlns:toolkit="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Toolkit"