我有以下CntrolTemplate有一個HierarchicalDataTemplate顯示我有一個自定義的數據類型。它只是一個帶有一個顯示按鈕和文本的stackPanel的ItemsControl。ItemsControl不包裝物品到下一行
我遇到的問題是,內容並不環繞,並轉到下一行時增加了很多項目:
<ControlTemplate x:Key="MyControlTemplate">
<StackPanel x:Name="MyStackPanel" Orientation="Horizontal" Width="Auto" Margin="0,1,0,1" Background="{x:Null}">
<ItemsControl x:Name="MyItemsControl" Margin="5,0,5,0" ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:MyClass}}, Path=ItemsSource}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" IsItemsHost="True">
<StackPanel.Resources>
<BooleanToVisibilityConverter x:Key="BoolToVisibility"/>
<HierarchicalDataTemplate DataType="{x:Type local:MyCustomDataType}">
<Button Height="24" MinWidth="16" Width="Auto" Command="{Binding}" Visibility="{Binding RelativeSource={RelativeSource Self}, Path=IsEnabled, Converter={StaticResource BoolToVisibility}}"
Background="Transparent" BorderBrush="Transparent">
<StackPanel Orientation="Horizontal" Height="22" VerticalAlignment="Center">
<Image Height="Auto" Width="Auto" Stretch="Uniform" Source="{Binding Path=IconSource}"
Visibility="{Binding Path=HideInTileGroup, Converter={StaticResource BoolToVisibility}}" Margin="2" VerticalAlignment="Center"/>
<TextBlock Text="{Binding Path=DisplayName}" VerticalAlignment="Center" Width="Auto"
Foreground="{Binding Path=DisplayBrush}"
Visibility="{Binding Path=ShowDisplayName, Converter={StaticResource BoolToVisibility}}" FontSize="12">
</TextBlock>
</StackPanel>
</Button>
</HierarchicalDataTemplate>
</StackPanel.Resources>
</StackPanel>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</StackPanel>
</ControlTemplate>
我怎樣才能使它環繞?
編輯:
我把它改成一個WrapPanel像這樣但還是不換行。
<ControlTemplate x:Key="MyControlTemplate">
<StackPanel x:Name="MyStackPanel" Orientation="Horizontal" Width="Auto" Margin="0,1,0,1" Background="{x:Null}">
<ItemsControl x:Name="MyItemsControl" Margin="5,0,5,0" ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:MyClass}}, Path=ItemsSource}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel>
<WrapPanel.Resources>
<BooleanToVisibilityConverter x:Key="BoolToVisibility"/>
<HierarchicalDataTemplate DataType="{x:Type local:MyCustomDataType}">
<Button Height="24" MinWidth="16" Width="Auto" Command="{Binding}" Visibility="{Binding RelativeSource={RelativeSource Self}, Path=IsEnabled, Converter={StaticResource BoolToVisibility}}"
Background="Transparent" BorderBrush="Transparent">
<StackPanel Orientation="Horizontal" Height="22" VerticalAlignment="Center">
<Image Height="Auto" Width="Auto" Stretch="Uniform" Source="{Binding Path=IconSource}"
Visibility="{Binding Path=HideInTileGroup, Converter={StaticResource BoolToVisibility}}" Margin="2" VerticalAlignment="Center"/>
<TextBlock Text="{Binding Path=DisplayName}" VerticalAlignment="Center" Width="Auto"
Foreground="{Binding Path=DisplayBrush}"
Visibility="{Binding Path=ShowDisplayName, Converter={StaticResource BoolToVisibility}}" FontSize="12">
</TextBlock>
</StackPanel>
</Button>
</HierarchicalDataTemplate>
</WrapPanel.Resources>
</WrapPanel>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</StackPanel>
使用WrapPanel而不是StackPanel? – Clemens
所以我的外層StackPanel是一個WrapPanel?是否保持內部ItemsControl相同? –
我的意思是ItemsPanel。 – Clemens