首页 > 其他分享 >WPF ListBox ListBoxItem Selected background style

WPF ListBox ListBoxItem Selected background style

时间:2024-09-05 22:14:01浏览次数:4  
标签:style set Windows Selected System booksList ListBoxItem using public

<Window x:Class="WpfApp340.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp340"
        mc:Ignorable="d" WindowState="Maximized"
        Title="MainWindow" Height="450" Width="800">
    <Window.Resources>
        <Style TargetType="ListBoxItem">
            <Setter Property="SnapsToDevicePixels" Value="True"/>
            <Setter Property="Padding" Value="2,0,0,0"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="ListBoxItem">
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsSelected" Value="True">
                                <Setter Property="Background" Value="Red" />
                                <Setter Property="Foreground" Value="White" />
                            </Trigger>
                        </ControlTemplate.Triggers>
                        <Border x:Name="Bd"
                                BorderBrush="{TemplateBinding BorderBrush}"
                                BorderThickness="{TemplateBinding BorderThickness}"
                                Background="{TemplateBinding Background}"
                                Padding="{TemplateBinding Padding}"
                                SnapsToDevicePixels="true"> 
                            <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                          VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                          SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>
    <Grid>
        <ListBox x:Name="lb" ItemContainerStyle="{StaticResource {x:Type ListBoxItem}}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Name}"/>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>
</Window>

 

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApp340
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        List<Book> booksList { get; set; }
        public MainWindow()
        {
            booksList = new List<Book>();
            InitializeComponent();
            for (int i = 0; i < 100; i++)
            {
                booksList.Add(new Book()
                {
                    Id = i + 1,
                    Name = $"Name_{i + 1}",
                    Title = $"Title_{i + 1}"
                });
            }
            lb.ItemsSource = booksList;
        }
    }

    public class Book
    {
        public int Id { get; set; }

        public string Name { get; set; }

        public string Title { get; set; }
    }
}

 

 

 

标签:style,set,Windows,Selected,System,booksList,ListBoxItem,using,public
From: https://www.cnblogs.com/Fred1987/p/18399316

相关文章

  • Class和Style与内联样式的绑定
    绑定HTMLClass示例对象语法我们可以传给 :class (v-bind:class 的简写)一个对象,实现动态地切换 class。也可以在对象中传入更多字段来动态切换多个 class。此外,v-bind:class 指令也可以与普通的 class 共存绑定内联样式:style的对象语法十分直观——看着非......
  • TPAMI 2024 | StyleTalk++:一个统一框架用于控制会说话头像的说话风格
    题目:StyleTalk++:AUnifiedFrameworkforControllingtheSpeakingStylesofTalkingHeadsStyleTalk++:一个统一框架用于控制会说话头像的说话风格作者:S.Wang;Y.Ma;Y.Ding;Z.Hu;C.Fan;T.Lv;Z.Deng;X.Yu摘要个体拥有独特的面部表情和头部姿势风格,这......
  • v-bind指令与class类和style的内联样式的绑定
    1.v-bind指令1.v-bind的认知与用法我们先创建一个新的页面去除掉一些元素保留剩下的元素然后对其添加一些属性和值创建好了之后我们可以来在页面上显示一个图片用我们的image组件找到我们的本地图片的位置或者网络图片的url放入我们的src里面。在src里面用到了我们的图片......
  • 重塑列表美学:CSS `list-style` 属性的魔法
    重塑列表美学:CSSlist-style属性的魔法摘要CSS的list-style属性是控制列表项标记样式的强大工具。通过这个属性,开发者可以轻松改变无序列表和有序列表的外观,包括标记的类型、位置以及图像。本文将深入探讨list-style属性的各个方面,并提供丰富的代码示例,展示如何使用......
  • [Javascript] Refactor blocking style code to stream style for fetching the strea
    WhenyouuseChatGPT,theresponsecomesinstream,sothatitcanappearsonscreenwheneverdatacomebackfromserver,wedon'tneedtowaitalldatacompletedthenshowingthedatatousers. Hereiscodewhichneedtobeimproved,becausethis......
  • 表达式用法,ref定义响应式,v-bind指令和图片轮播结合,class和style内联样式绑定,事件监听
    表达式用法当前时间,随机数,返回值,判断取值ref响应式使用ref赋值和普通赋值v-bind指令和图片轮播结合(v-bind可以省略成":")class和style内联样式绑定数据绑定一个常见需求是操作元素的class列表和它的内联样式两个class会用到这两个的样式,用v-bind对class里面的......
  • xlsx-js-style前端组装表格数据下载到本地
    1.数据格式:对象:key/valueconstdata1={'合同主体信息':'{\n"合同编号":"FWTsAEY-S1T-2023-01",\n"项目编号":"FTCG2023000265A",\n"项目名称":"福田区第二人民医院食堂服务采购项直",\n"总计/价款&q......
  • InstantStyleGaussian:利用3D Gaussian Splatting实现高效艺术风格迁移
    前言 本文介绍了InstantStyleGaussian,这是一种基于三维高斯飞溅(3DGS)场景表示的创新三维风格迁移方法。通过输入目标风格图像,它能够快速生成新的三维GS场景。方法作用于预先重建的GS场景,结合了扩散模型与改进的迭代数据集更新策略。它利用扩散模型生成目标风格图像,将这些新图像添......
  • 深入解析Objective-C中NSParagraphStyle的段落样式处理艺术
    标题:深入解析Objective-C中NSParagraphStyle的段落样式处理艺术在Objective-C的世界中,文本排版是一个复杂但至关重要的话题。NSParagraphStyle作为其中的核心组件,扮演着决定文本段落外观和布局的关键角色。本文将深入探讨NSParagraphStyle的内部机制,并通过实际代码示例,展示......
  • 摘要生成—通过摘要风格控制摘要的生成/抽取,原文阅读与理解:GEMINI: Controlling The S
    GEMINI:ControllingTheSentence-LevelSummaryStyleinAbstractiveTextSummarizationGEMINI:在抽象文本摘要中控制句子级摘要风格paper:https://arxiv.org/abs/2304.03548github:https://github.com/baoguangsheng/gemini本文介绍了一种自适应摘要抽取/生成方......