Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# wpf中的ItemsPanelTemplate选择器?_C#_Wpf - Fatal编程技术网

C# wpf中的ItemsPanelTemplate选择器?

C# wpf中的ItemsPanelTemplate选择器?,c#,wpf,C#,Wpf,我需要根据控件上的依赖项属性设置listbox的ItemsPanelTemplate属性。如何使用DataTemplateSelector来实现这一点 我有点像: <ListBox.ItemsPanel> <ItemsPanelTemplate> <!-- Here I need to replace with either a StackPanel or a wrap panel--> </ItemsPanelTempl

我需要根据控件上的依赖项属性设置listbox的ItemsPanelTemplate属性。如何使用DataTemplateSelector来实现这一点

我有点像:

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


谢谢

我认为最好的方法是为您的列表框使用样式,并设置触发器,根据您引用的DependencyProperty更改ItemsPanel。

没有
ItemsPanelSelector
(可能是因为它不是
数据模板
),但您可以绑定它或使用
触发器

绑定
示例


太好了。谢谢你提供代码示例。这真的帮了我很大的忙。向我的朋友竖起大拇指。
<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>