排列项目DataTemplate中的控制项目

本文关键字:项目 控制 控制项 DataTemplate 排列 | 更新日期: 2023-09-27 17:59:31

由于某些原因,在dataTemplate中添加的项不会按照我的指示执行!!

我试图在堆叠面板中水平放置多个图像,但无论我如何尝试,它们都只能垂直放置。

这是我的xaml。

<DataTemplate x:Name="View">
  <Border BorderBrush="Red" BorderThickness="4" >
      <StackPanel  Orientation="Horizontal">
             <ItemsControl ItemsSource="{Binding Path=_Collection, Mode=OneWay}" >
                 <ItemsControl.ItemTemplate>
                      <DataTemplate >
                          <Image Source="{Binding _Source}" />
                      </DataTemplate>
                 </ItemsControl.ItemTemplate>
             </ItemsControl>
         <TextBlock Height="30" FontFamily="Trebuchet MS" FontSize="18" Text="{Binding _Name}" />
      </StackPanel>
  </Border>
</DataTemplate>

一切都绑定好了。这是在用户控制范围内调用的正确

<ItemsControl ItemTemplate="{StaticResource siteView}" ItemsSource="{Binding Path=_SiteDisplay"/>

我的obervable集合_SiteDisplay包含另一个名为_collection的obervablecollection,它保存图像的url。

这是从实际代码中截取的,但说明了问题所在。我无法使图像水平对齐!非常感谢任何帮助,或者为更好的方法提供建议。

排列项目DataTemplate中的控制项目

您需要更改ItemsControl使用的面板,而不是包含ItemsControl:的面板

<ItemsControl ItemsSource="{Binding Path=_Collection, Mode=OneWay}" >
    <ItemsControl.ItemTemplate>
        <DataTemplate >
            <Image Source="{Binding _Source}" />
        </DataTemplate>
    </ItemsControl.ItemTemplate>
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate >
            <StackPanel Orientation="Horizontal" />
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
</ItemsControl>