Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Orientation of StackPanel is not Working in ItemsControl (WPF)

Tags:

c#

wpf

xaml

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>
like image 753
user2835256 Avatar asked Dec 05 '22 09:12

user2835256


1 Answers

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>
like image 128
Chris Avatar answered Mar 07 '23 21:03

Chris