为什么在 Silverlight 中 StackPanel 不将文本块放在左侧,按钮放在右侧?
好吧,我放弃了:我必须对下面的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用工具包中的扩展面板或使用具有 2 列的网格。 并将第二列的内容右对齐
you could use dockpanel from toolkit or use grid with 2 columns. and have the content of the second column right aligned
您的意思是希望按钮对齐到表单的右侧吗? 如果是这样,StackPanel 将不会这样做。 它可以水平或垂直地“堆叠”东西。
我建议您尝试 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:
我认为马特的方法是最好的。 不过,有两种选择:使用网格并将内容左右对齐,或者只给按钮留出很大的边距。
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.
参考应该是:
Reference should be: