使用 StackPanel 作为内容控件 (WPF)
所以我有一个 StackPanel,我将其用作 ContentControl。 我有一个地方希望根据我绑定的数据生成按钮,这一切都很好,但我希望按钮水平布局,而不是像当前正在发生的那样垂直布局。 这是屏幕截图:
这是我的 ContentTemplate 描述中的代码:
<StackPanel Name="wpReleaseButtons" Orientation="Horizontal" Grid.Row="2">
<ItemsControl IsTabStop="False" ItemsSource="{Binding Path=BranchCommands}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Button Tag="{Binding}" Padding="3">
<TextBlock Text="{Binding Path=DisplayValue}" />
</Button>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>
不确定我是什么我在这里做错了。 任何信息,将不胜感激。 谢谢!
So I have a StackPanel that I am using as a ContentControl. I have a place where I want buttons to be generated based on data that I am binding to, and that is all working good, but I want the buttons to be laid out horizontally, not vertically as is what is currently happening. Here's a screenshot:
And here is the code from my ContentTemplate description:
<StackPanel Name="wpReleaseButtons" Orientation="Horizontal" Grid.Row="2">
<ItemsControl IsTabStop="False" ItemsSource="{Binding Path=BranchCommands}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Button Tag="{Binding}" Padding="3">
<TextBlock Text="{Binding Path=DisplayValue}" />
</Button>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>
Not sure what I'm doing wrong here. Any info would be greatly appreciated. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我想说,看起来
ItemsControl
是垂直显示按钮的。 如果您希望ItemsControl
中的按钮是水平的,那么您需要将StackPanel
放在ItemsControl
ItemsPanelTemplate
中>,而不是像您在代码中那样相反:我在
ItemsControl.ItemsPanel
位上可能有点错误,因为我没有任何数据来测试它...编辑:除了 Bea 参考之外,WPF 博士。
I would say it looks like the
ItemsControl
is what is displaying the buttons vertically. if you want the buttons in theItemsControl
to be horizontal, then you need theStackPanel
to be in theItemsControl
ItemsPanelTemplate
, not the other way round like what you have in your code:I might be slightly wrong on the
ItemsControl.ItemsPanel
bit as I haven't got any data to test it with...Edit: In addition to the Bea reference, there's some good stuff by Dr WPF.
我看不到你的图像(它被我公司的防火墙阻止了),但无论如何我还是...
你的“Orientation =“Horizontal””可能按其应有的方式工作:它只包含一个子元素,一个 ItemsControl。 相反,尝试为您的 ItemsControl 创建一个 ControlTemplate,其中 ControlTemplate 包含一个 Orientation="Horizontal" 的 StackPanel。
希望这可以帮助!
编辑:
Bea 再次给出了答案/示例!
http://bea.stollnitz.com/blog/?p=10
I can't see your image (it's blocked by my company's firewall), but here I go anyways...
Your 'Orientation="Horizontal"' is probably working as it should: it only contains one child element, an ItemsControl. Instead, try making a ControlTemplate for your ItemsControl, where the ControlTemplate contains a StackPanel with Orientation="Horizontal".
Hope this helps!
Edit:
Once again, Bea comes through with an answer/example!
http://bea.stollnitz.com/blog/?p=10