ItemsControl DataTemplate Items showing side by side

百般思念 提交于 2019-12-14 02:11:31

问题


I've been researching this for quite long time now, can't find the answer.

How can I display each item in my itemscontrol side by side?

The following code displays each item's content side by side (label and textbox), but the next item is shown underneath. Let's say I have 3 items in my ItemsControl. The current behaviour is:

Label Textbox
Label Textbox
Label Textbox

What I want is:

Label Textbox Label Textbox Label Textbox (side by side)

The current code uses a stack panel whick sets the orientation to horizontal (that's why the label and textbox are side by side). But I need some property or technique to set the itemscontrol content orientation to horizontal. My code:

<ItemsControl.ItemTemplate>
    <DataTemplate>
          <StackPanel Name="pnlText" Orientation="Horizontal" Width="750">
              <Label Content="{Binding ParameterDisplayName, Mode=OneWay}" />
              <TextBox Name="txtText" HorizontalAlignment="Left" Text="{Binding ParameterValue, Mode=TwoWay, ValidatesOnExceptions=True, NotifyOnValidationError=True}" Visibility="{Binding ParameterType, Converter={StaticResource ParameterTypeToVisibilityConverter}, ConverterParameter=Text}" />
           </StackPanel>
    </DataTemplate>
</ItemsControl.ItemTemplate>

Does anyone know how to do that?

Thanks!


回答1:


You should set this property for your ItemsControl:

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


来源:https://stackoverflow.com/questions/8308903/itemscontrol-datatemplate-items-showing-side-by-side

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!