代码之家  ›  专栏  ›  技术社区  ›  Edward Tanguay

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

  •  11
  • Edward Tanguay  · 技术社区  · 16 年前

    好吧,我放弃了:我该怎么改呢 堆栈面板 下面是这样的:

    • 窗体最左边的文本
    • 窗体最右侧的按钮 .

    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>
    

    答:

    下载的 Silverlight 3 toolkit 其中包括DockPanel、Installed、Referenced 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 回复  |  直到 13 年前
        1
  •  11
  •   user122069    16 年前

    您可以使用工具箱中的DockPanel,也可以使用带2列的网格。并将第二列的内容右对齐

        2
  •  7
  •   Matt Hamilton    16 年前

    您的意思是要将按钮与表单右侧对齐吗?如果是这样,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>
    
        3
  •  1
  •   Jacob Adams    16 年前

    我认为马特的方法最好。不过,有两种选择是使用网格并将内容左右对齐,或者只给按钮一个很大的空白。

        4
  •  1
  •   sth    13 年前

    参考应为:

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