user2835256
user2835256

Reputation: 415

Orientation of StackPanel is not Working in ItemsControl (WPF)

My WPF Application is generating dynamic Buttons. I want to show these Button Horizontally. I wrote Code for this. Code is working fine but rather than showing button in Horizontal Direction, it is showing all Buttons in Vertical Direction! Where i also set Orientation of StackPanel!

Can anyone solve my Problem?

My Code is:

  <Grid>
    <dxlc:ScrollBox>
        <ItemsControl x:Name="Buttonslist">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <Button Content="{Binding Text}" Tag="{Binding Text}" x:Name="New" Margin="5,0,5,0" Click="New_Click"  />
    </StackPanel>
    </DataTemplate>
    </ItemsControl.ItemTemplate>
    </ItemsControl>

    </dxlc:ScrollBox>
    </Grid>

Upvotes: 5

Views: 4089

Answers (1)

Chris
Chris

Reputation: 5514

You're actually creating a StackPanel for each item/Button. To get just one for all the items you need to set the ItemsPanel of the control to a StackPanel.

<ItemsControl.ItemsPanel>
    <ItemsPanelTemplate>
        <StackPanel Orientation="Horizontal"/>
    </ItemsPanelTemplate>
</ItemsControl.ItemsPanel>

<ItemsControl.ItemTemplate>
    <DataTemplate>
        <Button Content="{Binding Text}" Tag="{Binding Text}" x:Name="New" Margin="5,0,5,0" Click="New_Click" />
    </DataTemplate>
</ItemsControl.ItemTemplate>

Upvotes: 18

Related Questions