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
WPF使用样式将ListBox WrapPannel更改为ListBox StackPanel_Wpf_Listbox_Styles - Fatal编程技术网

WPF使用样式将ListBox WrapPannel更改为ListBox StackPanel

WPF使用样式将ListBox WrapPannel更改为ListBox StackPanel,wpf,listbox,styles,Wpf,Listbox,Styles,我想将一个内部带有wrappanel的listview更改为一个内部带有stackpanel的listview,基本上在“小图像视图”和“细节视图”之间切换。 但我不确定最好的方法。到目前为止,我所拥有的: <UserControl.Resources> <Style x:Key="ListBoxWrap" TargetType="ListBox"> <Setter Property="ItemsPanel">

我想将一个内部带有wrappanel的listview更改为一个内部带有stackpanel的listview,基本上在“小图像视图”和“细节视图”之间切换。
但我不确定最好的方法。到目前为止,我所拥有的:

<UserControl.Resources>
    <Style x:Key="ListBoxWrap" TargetType="ListBox">
        <Setter Property="ItemsPanel">
            <Setter.Value>
                <ItemsPanelTemplate>
                    <WrapPanel />
                </ItemsPanelTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    <Style x:Key="ListBoxList" TargetType="ListBox">
        <Setter Property="ItemsPanel">
            <Setter.Value>
                <ItemsPanelTemplate>
                    <StackPanel />
                </ItemsPanelTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</UserControl.Resources>
        <ListBox Style="{StaticResource ListBoxList}" Name="lstContacts" Background="White" Margin="7,0,7,7" Grid.Row="1" ScrollViewer.HorizontalScrollBarVisibility="Disabled">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Border CornerRadius="4" Margin="5">
                        <StackPanel>
                            <TextBlock Text="{Binding FullName}" Margin="5,3,5,0" />
                            <TextBlock Text="{Binding Title}" Margin="5,0,5,3" />
                        </StackPanel>
                    </Border>
                </DataTemplate>
            </ListBox.ItemTemplate>
            <ListBox.ItemsPanel>
                <ItemsPanelTemplate>
                    <WrapPanel />
                </ItemsPanelTemplate>
            </ListBox.ItemsPanel>
        </ListBox>

你应该使用一个,然后你只需要换掉来改变外观,已经是一个细节视图,然后你只需要为缩略图创建一个视图

要这样做子类,文档中有一个示例;创建一个简单的缩略图视图应该不是很难


这种方法的优点是它完全封装了显示逻辑,因此除了面板之外,您不需要交换诸如
ItemTemplate
之类的属性。

您还可以使用ItemTemplateSelector根据可能发生的特定值更改来修改模板(事件)由鼠标悬停或单击事件触发

所有这些代码都将驻留在xaml中,您不需要创建单独的类或自定义控件