对于WPF/Silverlight布局,使用具有大量行和列或大量Stackpanel的Grid是否更好?
好的,我放弃了:我需要在下面更改为这个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>
Run Code Online (Sandbox Code Playgroud)
下载了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"> …Run Code Online (Sandbox Code Playgroud) 在下面的代码中,我告诉ComboBox通过分配其ItemTemplate属性来使用名为CustomerTemplate的DataTemplate .
但是,StackPanel没有ItemTemplate属性.
如何让StackPanel也使用CustomerTemplate?
<Window.Resources>
<DataTemplate x:Key="CustomerTemplate">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding FirstName}"/>
<TextBlock Text=" "/>
<TextBlock Text="{Binding LastName}"/>
</StackPanel>
</DataTemplate>
</Window.Resources>
<DockPanel LastChildFill="False" Margin="10">
<ComboBox
x:Name="CustomerList"
ItemTemplate="{StaticResource CustomerTemplate}"
HorizontalAlignment="Left"
DockPanel.Dock="Top"
Width="200"
SelectedItem="{Binding SelectedCustomer, Mode=TwoWay}"
ItemsSource="{Binding Customers}"/>
<StackPanel DataContext="{Binding SelectedCustomer}" Orientation="Horizontal">
<TextBlock Text="Chosen: "/>
<TextBlock Text="{Binding LastName}"/>
</StackPanel>
</DockPanel>
Run Code Online (Sandbox Code Playgroud) 我有一个StackPanel充满控件,我试图循环元素并获取他们的名称,但似乎我需要将每个元素转换为其类型以访问其Name属性.
但是如果我在StackPanel中有很多不同的类型并且我只想获取元素名称呢?
有没有更好的方法呢?
这是我尝试过的:
foreach (object child in tab.Children)
{
UnregisterName(child.Name);
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试创建一个动画,其中一个图标(Viewbox包装的Canvas上的xaml矢量图形)从它旁边的文本(TextBlock)到它下面.
我目前在水平方向的StackPanel中有Viewbox和TextBlock.我可以将方向更改为垂直方向,但这是一个即时更改(不是平滑动画).我也可以使用TranslateTransform(可以设置动画)来设置TextBlock的位置,但这很难以可重用的方式进行(即没有硬编码值).
任何人都能告诉我是否有任何以WPF为中心的方式来动画在堆栈面板中从水平方向到垂直方向的过渡?或者我没有想过要达到预期效果的另一种方式?
谢谢大家!
在构建用于数据输入的WPF表单时(例如,一堆文本框和组合框旁边的一堆标签)我看到了两种方法:
Height="auto"为每个字段添加行,为页眉和页脚添加两行(和提交按钮),每个标签和文本框都有自己的行.您如何设计数据输入表格?我目前在两种方法之间徘徊,也许还有一种我不知道的替代方案?
编辑:Henk说我应该定义最好,我认为我同意,最好的意思是最容易维护,创建,对齐,添加或删除字段,因为需求变化.
到目前为止,网格更好的唯一标准是易于对齐.
我遇到了一些问题,创建了一个非常类似于相机应用程序中的照片条的效果.
我想要做的就是显示一行网格,每个网格都有与屏幕相同的尺寸(无论是纵向还是横向).我必须做一些hacky并创建依赖属性,网格宽度和高度属性绑定到这些属性以保持宽高比.
这很好用.但是当我为我的条带创建一个StackPanel并实现我的导航(或者只是使用z-index变换缩放)时,我发现我的StackPanel不能显示大于屏幕尺寸(它被裁剪为只有一个网格的大小) .我以为我找到了一篇描述这个问题的帖子,但我现在找不到了 - 如果您知道我正在考虑哪个帖子,或者您对此限制有更多了解,请发帖.
我发现的唯一解决方法是使用ScrollViewer,这绝对不是我想要的行为,但它允许StackPanel比屏幕宽.
我真正的问题是ScrollViewer的行为 - 因为我需要从网格跳到网格(就像照片条那样)而不是自由滚动,据我所知,HorizontalOffset不是一个可动画的属性.我可以通过每15毫秒调用ScrollToHorizontalOffset强制它动画,基本上手动实现我自己的缓动效果.这看起来像是一个巨大的黑客,而且行为非常微不足道(要么我每次都没有得到ManipulationCompleted事件 - 在每次滑动操作结束时 - 或者ScrollViewer的内置惯性物理干扰我的效果).
有没有人知道我遇到的问题的更好的解决方法,或者在Silverlight中获得相机照片条的体验的完全不同的方式?
我考虑过使用Pivot控件,但它并不是我想要的(如果我希望每个项目在下一个项目进入之前完全动画化,而不是看起来全部附加到一个条带上,那么应该有更少的约束实现这一目标的方法).更重要的是,条带只是我希望能够动态执行的众多效果之一.我想交替使用类似CoolIris的3D倾斜或FlipPad风格的翻页.我相信如果我能够很好地使我当前的设置工作,那么很容易实现这些其他效果(作为可变转换).致力于像Pivot这样的控制不会让我更接近这个愿景.
这是我的XAML:
<Grid x:Name="LayoutRoot" Background="Transparent" Height="{Binding RealHeight, ElementName=phoneApplicationPage}" Width="{Binding RealWidth, ElementName=phoneApplicationPage}" HorizontalAlignment="Left" VerticalAlignment="Top">
<ScrollViewer x:Name="SlideScroller" VerticalScrollBarVisibility="Disabled" Height="{Binding RealHeight, ElementName=phoneApplicationPage}" Margin="0,0,0,-31" ScrollViewer.HorizontalScrollBarVisibility="Auto" HorizontalAlignment="Left" VerticalAlignment="Top">
<StackPanel x:Name="SlidePanel" Orientation="Horizontal" Height="{Binding RealHeight, ElementName=phoneApplicationPage}" VerticalAlignment="Top" HorizontalAlignment="Left">
<Grid x:Name="Slide0" Margin="0" VerticalAlignment="Top" HorizontalAlignment="Left" Width="{Binding RealWidth, ElementName=phoneApplicationPage}" Height="{Binding RealHeight, ElementName=phoneApplicationPage}" Background="#FFCCCCCC">
<Image x:Name="Photo0" Width="{Binding RealWidth, ElementName=phoneApplicationPage}" Height="{Binding RealHeight, ElementName=phoneApplicationPage}" VerticalAlignment="Top" HorizontalAlignment="Left" Stretch="UniformToFill"/>
</Grid>
<Grid x:Name="Slide1" Margin="0" VerticalAlignment="Top" HorizontalAlignment="Left" Width="{Binding RealWidth, ElementName=phoneApplicationPage}" …Run Code Online (Sandbox Code Playgroud) 我的google和stackoverflow search-fu让我失望了,所以我向社区提出了这个问题.
(这是使用VS2010和.NET 4.0生成的,在一个空白的默认WPF解决方案中)
考虑以下XAML:
<StackPanel Orientation="Horizontal">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="20"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Border Name="aborder" Grid.Column="0" Grid.ColumnSpan="2"
Background="Red" Width="200"/>
<Border Name="aborder2" Background="Green"/>
</Grid>
</StackPanel>
Run Code Online (Sandbox Code Playgroud)
你会预测"aborder2"的宽度是多少?
如果你猜到"20像素",你就错了.正确的答案是110像素.
考虑一下这个XAML:
<StackPanel Orientation="Horizontal">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="20"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Border Name="aborder" Grid.Column="0" Grid.ColumnSpan="2"
Background="Red" Width="200"/>
<Border Name="aborder2" Background="Green"/>
</Grid>
</StackPanel>
Run Code Online (Sandbox Code Playgroud)
你会预测"aborder2"的宽度是多少?
如果你猜到20像素或110像素,你就错了.正确答案是200像素.
我无法弄明白这一点,这让我感到疯狂.似乎答案应该是显而易见的; 显然,自动填充网格列和堆栈面板之间存在一些相互作用,导致网格发生故障.但它似乎没有意义 - 无论规则如何,这种行为似乎都是武断的.为何110像素?为什么不是109像素或100像素?我会理解,如果自动调整大小的列无法完全展开或者某些东西,但是为了让固定宽度列随机忽略它的宽度,我会给开发人员留下一个烧焦的外壳.
任何帮助或指导灯将非常感谢!
我如何在CheckBox中包装文本?文本比页面宽度略长,但我不能缩短它.CheckBox没有TextWrapping属性.;(
我也尝试设置宽度="460",宽度="*",但我没有成功.
<StackPanel>
<CheckBox Content="Lorem ipsum dolor sit amet, consectetur adipisicing elit." />
</StackPanel>
Run Code Online (Sandbox Code Playgroud) 在WPF中横向排列按钮的最佳控件是什么?在调整窗口大小时,我无法均匀地排列按钮.我试图使用堆栈面板,但按钮只是保持在他们的位置,并没有空间填充窗口空间.试图使用停靠面板,但没有运气.
任何人都可以建议我这样做的好方法或告诉我,如果我做错了什么?
stackpanel ×10
wpf ×8
grid ×3
silverlight ×3
xaml ×3
c# ×1
checkbox ×1
dockpanel ×1
forms ×1
itemtemplate ×1
layout ×1
orientation ×1
width ×1
word-wrap ×1
xml ×1