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

发布于 2024-07-24 05:05:12 字数 3784 浏览 10 评论 0原文

好吧,我放弃了:我必须对下面的 StackPanel 进行哪些更改,以便将:

  • 文本放在表单的最左侧
  • 按钮放在表单的最右侧

替代文本 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 工具包,其中包含 DockPanel ,已安装,引用 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

OK, I give up: what do I have to change to this StackPanel below so that it puts the:

  • text on far left of form
  • button on far right of form.

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>

ANSWER:

Downloaded Silverlight 3 toolkit which has DockPanel, installed, referenced System.Windows.Controls, then following 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

栀梦 2024-07-31 05:05:12

您可以使用工具包中的扩展面板或使用具有 2 列的网格。 并将第二列的内容右对齐

you could use dockpanel from toolkit or use grid with 2 columns. and have the content of the second column right aligned

穿越时光隧道 2024-07-31 05:05:12

您的意思是希望按钮对齐到表单的右侧吗? 如果是这样,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>

Do you mean that you want the button aligned to the right of the form? If so, StackPanel won't do that. It's made to "stack things up" either horizontally or vertically.

I would suggest you try 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>
仅此而已 2024-07-31 05:05:12

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

I think Matt's approach it the best. Two alternatives though are to use a grid and align the content to the left and right or to just give the button a really large margin.

撩动你心 2024-07-31 05:05:12

参考应该是:

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

Reference should be:

xmlns:toolkit="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Toolkit"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文