wpf 中的 ItemsPanelTemplate 选择器?

2023-12-28

我需要根据控件上的依赖属性设置列表框的 ItemsPanelTemplate 属性。如何使用 DataTemplateSelector 来做到这一点?

我有类似的东西:

<ListBox.ItemsPanel>
    <ItemsPanelTemplate>
        <!-- Here I need to replace with either a StackPanel or a wrap panel-->
    </ItemsPanelTemplate>
</ListBox.ItemsPanel>

Thanks


没有一个ItemsPanelSelector(可能是因为它不是DataTemplate)但你可以绑定它或使用Trigger

Binding example

<ListBox ItemsPanel="{Binding RelativeSource={RelativeSource Self},
                              Path=Background,
                              Converter={StaticResource MyItemsPanelConverter}}">

Trigger in Style example

<ListBox ItemsSource="{Binding Source={x:Static Fonts.SystemFontFamilies}}">
    <ListBox.Style>
        <Style TargetType="ListBox">
            <Setter Property="ItemsPanel">
                <Setter.Value>
                    <ItemsPanelTemplate>
                        <StackPanel/>
                    </ItemsPanelTemplate>
                </Setter.Value>
            </Setter>
            <Style.Triggers>
                <!-- Your Trigger.. -->
                <Trigger Property="Background" Value="Green">
                    <Setter Property="ItemsPanel">
                        <Setter.Value>
                            <ItemsPanelTemplate>
                                <WrapPanel/>
                            </ItemsPanelTemplate>
                        </Setter.Value>
                    </Setter>
                </Trigger>
            </Style.Triggers>
        </Style>
    </ListBox.Style>
</ListBox>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

wpf 中的 ItemsPanelTemplate 选择器? 的相关文章

随机推荐