【问题标题】:WPF Using styles to change a ListBox WrapPannel to a ListBox StackPanelWPF 使用样式将 ListBox WrapPanel 更改为 ListBox StackPanel
【发布时间】:2011-05-11 21:19:55
【问题描述】:

我想将一个带有包装面板的列表视图更改为一个带有堆栈面板的列表视图,基本上是在“小图像视图”和“详细信息视图”之间切换。
虽然不确定最好的方法。到目前为止我所拥有的:

<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>

【问题讨论】:

    标签: wpf listbox styles


    【解决方案1】:

    你应该使用ListView,然后你只需要换掉ListView.View来改变外观,GridView已经是一个细节视图,那么你只需要为缩略图。

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

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

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-04-06
        • 1970-01-01
        • 2015-08-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多