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>
复制代码

 

posted on   lopengye  阅读(620)  评论(0编辑  收藏  举报

编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示