c# - wpf 中的 ItemsPanelTemplate 选择器?

标签 c# wpf

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

我有这样的东西:

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

谢谢

最佳答案

没有 ItemsPanelSelector(可能是因为它不是 DataTemplate),但您可以绑定(bind)它或使用 Trigger

绑定(bind)示例

<ListBox ItemsPanel="{Binding RelativeSource={RelativeSource Self},
                              Path=Background,
                              Converter={StaticResource MyItemsPanelConverter}}">
样式示例中的

触发器

<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>

关于c# - wpf 中的 ItemsPanelTemplate 选择器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10652639/

相关文章:

wpf - 如何从 XAML 中为样式中的控件指定工具提示?

TabControl 中的 WPF UI 持久性

c# - 制作一个完全透明的矩形(窗口中的一个洞)WPF

c# - DataGrid 行标题 WPF

c# - 匹配客户在前端选择的搜索属性

c# - 如何在 c# 中使用 vmware sdk 设置/更改虚拟机的 SCSI Controller 类型

c# - 使用通用参数的字符串名称构造类

WPF如何控制ListCollectionView的绑定(bind)方法?

c# - 获取文件的相对路径 C#

c# - 从单元测试调用调度程序时传播在继续任务中引发的异常