ItemsControl不将项目包装到下一行

时间:2016-12-04 13:48:20

标签: c# wpf xaml

我有以下CntrolTemplate,它有一个显示我拥有的自定义DataType的HierarchicalDataTemplate。它只是一个带有stackPanel的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>

1 个答案:

答案 0 :(得分:1)

如果将var orderRows = [1,2,3,4]; for (var i = 0; i < orderRows.length; i++) { prestan.get('combinations', {"id": orderId}).then(function (combinationsResponse) { var s=orderRows[i]; 更改为ItemsPanel无效,那是因为您还有一个外WrapPanel

StackPanel

这意味着<ControlTemplate x:Key="MyControlTemplate"> <StackPanel x:Name="MyStackPanel" Orientation="Horizontal" Width="Auto" Margin="0,1,0,1" Background="{x:Null}"> 仍然认为它有无限宽度可以处理,所以实际上不会包裹。

完全移除外部ItemsPanel,将其替换为其他容器或限制其宽度。我的代码中没有看到任何内容告诉我你需要外部容器,所以我只需将其移除并从那里开始。

如果它还没有包裹,那么在视觉树中的某个地方仍然必须有另一个“无限宽度”(即一个增长以适应它的内容)的容器。你需要从这个项目向外工作,直到找到它为止。