WPF使用样式将ListBox WrapPannel更改为ListBox StackPanel
我想将一个内部带有wrappanel的listview更改为一个内部带有stackpanel的listview,基本上在“小图像视图”和“细节视图”之间切换。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">
但我不确定最好的方法。到目前为止,我所拥有的:
<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中,您不需要创建单独的类或自定义控件