开发者

WPF ItemsPanelTemplate not working

开发者 https://www.devze.com 2022-12-12 21:52 出处:网络
I\'m trying to get an ItemsPanelTemplate working for a ListBox. The ListBox is used in a DataTemplate, and none of my implicit ListBox styles override the default visual style. For some reason, the It

I'm trying to get an ItemsPanelTemplate working for a ListBox. The ListBox is used in a DataTemplate, and none of my implicit ListBox styles override the default visual style. For some reason, the ItemsPanelTemplate I'm specifiying for the control (a WrapPanel) is being ignored and a StackPanel is used instead.

This is the entire template, I can only assume there's something I'm missing which is causing this.

<DataTemplate x:Key="GroupLargeIconsTemplate" DataType="{x:Type Core:IGroup}">
    <ListBox ItemsSource="{Binding Children}" OverridesDefaultStyle="True">
        <ListBox.ItemsPanel>
            <ItemsPanelTemplate>
                <WrapPanel HorizontalAlignment="Left" VerticalAlignment="Top" IsItemsHost="True"  ScrollViewer.HorizontalScrollBarVisibility="Disabled" ScrollViewer.VerticalScrollBarVisibility="Auto"
                  开发者_运维技巧         Width="{Binding (FrameworkElement.ActualWidth), RelativeSource={RelativeSource AncestorType=ScrollContentPresenter}}" />
            </ItemsPanelTemplate>
        </ListBox.ItemsPanel>
        <ListBox.ItemTemplate>
            <DataTemplate>
                <DataTemplate.Resources>
                    <conv:IconConverter x:Key="IconConverter"/>
                </DataTemplate.Resources>
                <StackPanel Margin="2" Width="100" Height="140">
                    <Image Source="{Binding Icon,Converter={StaticResource IconConverter},ConverterParameter=96}"/>
                    <TextBlock Text="{Binding Name}" TextWrapping="Wrap" TextTrimming="CharacterEllipsis" />
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</DataTemplate>


My guess is that the width property in the WrapPanel is making it behave as a Stackpanel.


Strange, the control template is not supposed to override the explicitly specified values, rather the opposite scenario comes to my mind... That is, unless the control template provides a panel with "IsItemsHost" set to true. Then the ItemsPanel is ignored. Which, probably, is your case.

0

精彩评论

暂无评论...
验证码 换一张
取 消