2009-06-01 42 views
5

所以我有一个StackPanel,我用作一个ContentControl。我有一个地方,我希望根据绑定的数据生成按钮,而且这一切都很好,但我希望按钮水平放置,而不是像现在发生的那样垂直放置。下面是截图:使用StackPanel作为ContentControl(WPF)

alt text

这里是从我的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> 

不知道我在做什么错在这里。任何信息,将不胜感激。谢谢!

回答

9

我会说,它看起来像ItemsControl就是垂直显示的按钮。如果你想在ItemsControl的按钮是水平的,那么你需要的StackPanel是在ItemsControlItemsPanelTemplate,而不是反过来就像你在你的代码是什么:

<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位,因为我还没有得到任何数据与...

编辑来测试它:除了衣基准,有通过Dr WPF一些好东西。

+0

这样做了!感谢您的回答,并且我还为Bea的网站添加了书签。感谢大家!这开始让我感到不安,哈哈。 – 2009-06-01 14:51:17

3

我看不到你的形象(它已被我公司的防火墙),但在这里我去反正...

你“方向=‘横向’”可能是因为它的工作应:它只包含一个子元素,一个ItemsControl。相反,请尝试为您的ItemsControl制作一个ControlTemplate,其中ControlTemplate包含一个Orientation =“Horizo​​ntal”的StackPanel。

希望这会有所帮助!

编辑:

再次

,衣来通过一个答案/例子!

http://bea.stollnitz.com/blog/?p=10

+0

+1为Bea参考。 – 2009-06-01 14:18:54