在运行时更改ListBox's ItemsPanelTemplate
本文关键字:ItemsPanelTemplate ListBox 运行时 | 更新日期: 2023-09-27 18:15:00
我想让ListBox的ItemsPanelTemplate在运行时改变。
我有以下XAML,它允许我改变ItemsPanelTemplate;但是有破坏ScrollViewer的副作用。
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:ie="clr-namespace:Microsoft.Expression.Interactivity.Core;assembly=Microsoft.Expression.Interactions"
...
<UserControl.Resources>
<ItemsPanelTemplate x:Key="StackPanelTemplate">
<VirtualizingStackPanel/>
</ItemsPanelTemplate>
<ItemsPanelTemplate x:Key="WrapPanelTemplate">
<telerik:RadWrapPanel/>
</ItemsPanelTemplate>
</UserControl.Resources>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<StackPanel>
<Button Content="StackPanel">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<ie:ChangePropertyAction TargetName="TargetListBox" PropertyName="ItemsPanel" Value="{StaticResource StackPanelTemplate}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>
<Button Content="WrapPanel">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<ie:ChangePropertyAction TargetName="TargetListBox" PropertyName="ItemsPanel" Value="{StaticResource WrapPanelTemplate}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>
</StackPanel>
<ListBox x:Name="TargetListBox" Grid.Column="1" ItemsSource="{Binding SomeCollection}"/>
</Grid>
当你这样修改ItemsPanelTemplate时。ScrollViewer似乎保持在您更改它之前的任何状态,并且使用滚动条不会影响ListBox上的任何更改。
谁能就这个问题提供任何见解,或者提供一个解决方案?
谢谢。
* EDIT *
所以,我已经把问题缩小到与虚拟化有关。如果你把VirtualizingStackPanel改成一个普通的StackPanel, ScrollViewer不会中断。这对我来说并不是一个真正的解决方案,因为这个ListBox将容纳数百个搜索结果。
我认为最简单的解决方法是替换整个ListBox,而不仅仅是面板模板。
好吧,我面临着同样的问题,我想创建一个ListBox,有产品,让用户自由地改变布局从WrapPanel到列表框和从ListBox到WrapPanel。要做到这一点,你应该使用样式。(我建议你使用ListView,而不是ListBox,因为在ListBox滚动问题。无论如何,两者都可以工作)。首先在app.xaml中添加你的2个样式。
WrapPanelTemplateLV
<Style x:Key="WrapPanelTemplateLV" TargetType="ListView">
<Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Disabled" />
<Setter Property="ItemsPanel">
<Setter.Value>
<ItemsPanelTemplate>
<WrapPanel />
</ItemsPanelTemplate>
</Setter.Value>
</Setter>
</Style>
和StackPanelTemplateLV
<Style x:Key="StackPanelLV" TargetType="ListBox">
<Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Disabled" />
<Setter Property="ItemsPanel">
<Setter.Value>
<ItemsPanelTemplate>
<StackPanel />
</ItemsPanelTemplate>
</Setter.Value>
</Setter>
</Style>
现在在你的按钮里面做这个
// StackPanelLV is on my App.xaml
MyListView.Style = (Style)Application.Current.Resources["StackPanelLV"];
,现在你明白了。制作一些逻辑在两种风格之间切换。我建议你去看看这个问题。在WPF运行时更改样式