首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >wpf中的ItemsPanelTemplate选择器?

wpf中的ItemsPanelTemplate选择器?
EN

Stack Overflow用户
提问于 2012-05-18 20:26:20
回答 2查看 4.8K关注 0票数 8

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

我有一些类似的东西:

代码语言:javascript
复制
<ListBox.ItemsPanel>
    <ItemsPanelTemplate>
        <!-- Here I need to replace with either a StackPanel or a wrap panel-->
    </ItemsPanelTemplate>
</ListBox.ItemsPanel>

谢谢

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-05-18 21:28:37

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

Binding示例

代码语言:javascript
复制
<ListBox ItemsPanel="{Binding RelativeSource={RelativeSource Self},
                              Path=Background,
                              Converter={StaticResource MyItemsPanelConverter}}">

Style中的Trigger示例

代码语言:javascript
复制
<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>
票数 17
EN

Stack Overflow用户

发布于 2012-05-18 21:28:05

我认为这里最好的方法是为你的ListBox使用样式,并设置触发器,根据你引用的DependencyProperty更改ItemsPanel。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10652639

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档