WPF ListView ListBox 常用的样式记录

Posted lopengye

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了WPF ListView ListBox 常用的样式记录相关的知识,希望对你有一定的参考价值。

ListView:

<ListView x:Name="lvBlockedApps"
                        ItemsSource="{Binding BlockedAppsCollecion}">
                        <ListView.Style>
                            <Style TargetType="ListView">
                                <Setter Property="VerticalAlignment" Value="Top"></Setter>
                                <Setter Property="HorizontalAlignment" Value="Stretch"></Setter>
                                <Setter Property="SelectionMode" Value="Single"></Setter>

                                <Setter Property="Template">
                                    <Setter.Value>
                                        <ControlTemplate TargetType="{x:Type ListView}">
                                            <ScrollViewer VerticalScrollBarVisibility="Visible" HorizontalScrollBarVisibility="Auto" HorizontalAlignment="Stretch">
                                                <ItemsPresenter/>
                                            </ScrollViewer>
                                        </ControlTemplate>
                                    </Setter.Value>
                                </Setter>

                            </Style>
                        </ListView.Style>
                        <ListView.ItemContainerStyle>
                            <Style TargetType="{x:Type ListViewItem}">
                                <Setter Property="Height" Value="40"></Setter>
                                <Setter Property="Margin" Value="5,2,3,0"></Setter>
                                <Setter Property="BorderBrush" Value="Red"></Setter>
                                <Setter Property="BorderThickness" Value="2"/>
                                <Setter Property="Cursor" Value="Hand"></Setter>
                                <Setter Property="Template">
                                    <Setter.Value>
                                        <ControlTemplate TargetType="{x:Type ListViewItem}">
                                            <Border x:Name="back" BorderBrush="Green" BorderThickness="0" >
                                                <Grid>
                                                    <Grid.ColumnDefinitions>
                                                        <ColumnDefinition Width="Auto"></ColumnDefinition>
                                                        <ColumnDefinition Width="*"></ColumnDefinition>
                                                        <ColumnDefinition Width="Auto"></ColumnDefinition>
                                                    </Grid.ColumnDefinitions>
                                                    <Border Grid.Column="0" Width="32" Height="32" CornerRadius="5" Background="#FF83CAD1" Margin="5,0,0,0">
                                                        <Border>
                                                            <Border.Background>
                                                                <ImageBrush ImageSource="{Binding AppIcon,Converter={StaticResource ImgPathToImageConverter}}"></ImageBrush>
                                                            </Border.Background>
                                                        </Border>
                                                    </Border>
                                                    <customControl:EnhancedTextBlock Grid.Column="1" Margin="10,0,0,0" FontSize="16" Text="{Binding Name}" />
                                                    <Button Grid.Column="2"
                                                        x:Name="btnUnblock"
                                                        Width="60" Height="23"
                                                        Content="Unblock"
                                                        Padding="0"
                                                        Margin="0,0,8,0"
                                                        Style="{DynamicResource FileDialogButtonStyle}"
                                                        Click="btnUnblock_Click"></Button>
                                                </Grid>
                                            </Border>
                                            <ControlTemplate.Triggers>
                                                <Trigger Property="IsMouseOver" Value="True">
                                                    <Setter TargetName="back" Property="Background" Value="LightGray"></Setter>
                                                </Trigger>
                                                <Trigger Property="IsSelected" Value="True">
                                                    <Setter TargetName="back" Property="Background" Value="Gray"></Setter>
                                                </Trigger>
                                            </ControlTemplate.Triggers>
                                        </ControlTemplate>
                                    </Setter.Value>
                                </Setter>

                            </Style>
                        </ListView.ItemContainerStyle>
                    </ListView>

ListBox:
                    <ListBox x:Name="lbPageMenus" ItemsSource="{Binding PageMenus}" SelectionMode="Single">
                        <ListBox.Template>
                            <ControlTemplate TargetType="{x:Type ListBox}">
                                <ItemsPresenter/>
                            </ControlTemplate>
                        </ListBox.Template>
                        <ListBox.ItemsPanel>
                            <ItemsPanelTemplate>
                                <WrapPanel Orientation="Horizontal" IsItemsHost="True"/>
                            </ItemsPanelTemplate>
                        </ListBox.ItemsPanel>
                        <ListBox.ItemContainerStyle>
                            <Style TargetType="ListBoxItem">
                                <Setter Property="Margin" Value="0,0,50,0"></Setter>
                                <Setter Property="Cursor" Value="Hand"></Setter>
                                <Setter Property="IsSelected" Value="{Binding IsSelected}"></Setter>
                                <Setter Property="IsEnabled" Value="{Binding IsEnabled}"></Setter>
                                <Setter Property="Template">
                                    <Setter.Value>
                                        <ControlTemplate TargetType="{x:Type ListBoxItem}">
                                            <Border x:Name="back" BorderBrush="Transparent" BorderThickness="0,0,0,5"  Width="{Binding Width}" Height="{Binding Height}">
                                                    <TextBlock x:Name="text"
                                                        Text="{Binding DisplayName}"
                                                        VerticalAlignment="Center"
                                                        HorizontalAlignment="Center"
                                                        FontSize="22"
                                                        Foreground="White"></TextBlock>
                                                </Border>
                                            <ControlTemplate.Triggers>
                                                <Trigger Property="IsSelected" Value="True">
                                                    <Setter TargetName="text" Property="Foreground" Value="#FF47F5F5"></Setter>
                                                    <Setter TargetName="back" Property="BorderBrush" Value="#FF47F5F5"></Setter>
                                                </Trigger>
                                                <Trigger Property="IsEnabled" Value="False">
                                                    <Setter TargetName="text" Property="Foreground" Value="#FF89D7D4"></Setter>
                                                    <Setter TargetName="back" Property="BorderBrush" Value="Transparent"></Setter>
                                                </Trigger>
                                            </ControlTemplate.Triggers>
                                        </ControlTemplate>
                                    </Setter.Value>
                                </Setter>
                            </Style>
                        </ListBox.ItemContainerStyle>
                    </ListBox>

 

以上是关于WPF ListView ListBox 常用的样式记录的主要内容,如果未能解决你的问题,请参考以下文章

WPF ListBox/ListView/DataGrid 列表滚动与虚拟化

2022-03-23 WPF面试题 ListBox 与 ListView - 如何选择以及何时进行数据绑定?

2022-03-23 WPF面试题 ListBox 与 ListView - 如何选择以及何时进行数据绑定?

〝WPF〞中的〝ListBox〞、〝ListView〞和〝DataGridView〞有啥区别?

WPF中的ListBox,ListView和DataGridView有啥区别

UI响应性并使用WPF中的“SelectedItem”ListView / ListBox