3

我想在运行时更改 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 上的任何更改。

任何人都可以提供有关此问题的任何见解或提供解决方法吗?

谢谢你。

* 编辑 *

因此,我将问题缩小到与虚拟化有关的问题。如果您仅将 VirtualizingStackPanel 更改为常规 StackPanel,则 ScrollViewer 不会中断。这对我来说并不是一个真正的解决方案,因为这个 ListBox 将容纳数百个搜索结果。

4

2 回答 2

1

我认为最简单的解决方法是替换整个 ListBox 而不仅仅是面板模板。

于 2011-11-17T03:02:39.817 回答
0

好吧,我遇到了同样的问题,我想创建一个包含产品的 ListBox,让用户可以自由地将布局从 WrapPanel 更改为 List box,从 ListBox 更改为 WrapPanel。所以要做到这一点,你应该使用样式。(由于 ListBox 中的滚动问题,我建议您使用 ListView 而不是 ListBox。无论如何两者都可以使用)。首先在里面添加你的2种样式在你的app.xaml

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 中运行时更改样式

于 2017-07-04T13:44:51.263 回答