开发者

WPF Using styles to change a ListBox WrapPannel to a ListBox StackPanel

I want to change a listview with a wrappanel inside into a listivew with a stackpanel inside, basically to switch between a "small image view" and a "details view".

Not sure the best way to do it though. What I have so far:

<UserControl.Resources>
    <Style x:Key="ListBoxWrap" TargetType="ListBox">
        <Setter Property="ItemsPanel">
            <Setter.Value>
                <ItemsPanelTemplate>
                    <WrapPanel />
                </ItemsPanelTemplate>
            </Setter.Value>
        </Sette开发者_如何转开发r>
    </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>


You should use a ListView for that, then you just need to swap out the ListView.View to change the appearance, the GridView already is a details-view, then you only need to create a view for the thumbnails.

To do so subclass ViewBase, there is an example in the documentation; creating a simple thumbnail view should not be very hard.

This approach has the advantage that it fully encapsulates the display-logic, so you should not need to swap out properties like ItemTemplate in addition to the panel.


You could also use a ItemTemplateSelector to modify the template based on a specific value change which could be (event) triggered by a mouse-over or click event.

All of this code would reside in the xaml and you would not need to create a separate class or custom control.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜