使用StackPanel中为ContentControl中(WPF)中为、StackPanel、WPF、ContentControl

2023-09-03 16:36:59 作者:糖甜到哀伤

所以,我有我使用一个ContentControl中一的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:

和这里是我的ContentTemplate说明code:

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>

不知道我在做什么错在这里。任何信息将是极大的AP preciated。谢谢!

Not sure what I'm doing wrong here. Any info would be greatly appreciated. Thanks!

推荐答案

我会说,它看起来像的ItemsControl 是垂直显示的按钮。如果你想在按钮的ItemsControl 是水平的,那么你需要的的StackPanel 是在的ItemsControl ItemsPanelTemplate ,而不是倒过来像你在你的code:

I would say it looks like the ItemsControl is what is displaying the buttons vertically. if you want the buttons in the ItemsControl to be horizontal, then you need the StackPanel to be in the ItemsControl ItemsPanelTemplate, not the other way round like what you have in your code:

<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.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel Orientation="Horizontal"/>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
</ItemsControl>

我可能会稍微错了就 ItemsControl.ItemsPanel 位,因为我还没有得到任何数据与...

I might be slightly wrong on the ItemsControl.ItemsPanel bit as I haven't got any data to test it with...

编辑:除了BEA的参考,有一些好东西通过的博士WPF 。

In addition to the Bea reference, there's some good stuff by Dr WPF.

 
精彩推荐
图片推荐