首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在运行时更改ListBox的ItemsPanelTemplate

在运行时更改ListBox的ItemsPanelTemplate
EN

Stack Overflow用户
提问于 2011-11-17 02:50:00
回答 2查看 1.5K关注 0票数 3

我希望在运行时更改ListBox的ItemsPanelTemplate。

我有以下XAML,它允许我更改ItemsPanelTemplate;但是,它有破坏ScrollViewer的副作用。

代码语言:javascript
复制
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将保存数百个搜索结果。

EN

回答 2

Stack Overflow用户

发布于 2011-11-17 03:02:39

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

票数 1
EN

Stack Overflow用户

发布于 2017-07-04 13:44:51

嗯,我也面临着同样的问题,我想要创建一个有产品的ListBox,让用户可以自由地将布局从WrapPanel更改为列表框,从ListBox更改为WrapPanel。因此,要做到这一点,您应该使用样式。(我建议您使用ListView而不是ListBox,因为ListBox中存在滚动问题。不管怎样,两者都会起作用的)。首先,在app.xaml中添加2种样式

WrapPanelTemplateLV

代码语言:javascript
复制
<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

代码语言:javascript
复制
<Style x:Key="StackPanelLV" TargetType="ListBox">
            <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Disabled" />
            <Setter Property="ItemsPanel">
                <Setter.Value>
                    <ItemsPanelTemplate>
                        <StackPanel />
                    </ItemsPanelTemplate>
                </Setter.Value>
            </Setter>
        </Style>

现在在你的按钮里做这个

代码语言:javascript
复制
// StackPanelLV is on my App.xaml
            MyListView.Style = (Style)Application.Current.Resources["StackPanelLV"];

现在你有了主意。在两种样式之间转换一些逻辑。我建议你多问这个问题。Changing the styles at runtime in WPF

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8161504

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档