设置之后的效果为

1 窗体中代码

<Window x:Class="QyNodeTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Style/Style.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Window.Resources>
    <Grid>
        <ListBox Style="{StaticResource ListBoxHor}">
            <ListBoxItem>
                <Border>
                    <StackPanel>
                        <TextBlock HorizontalAlignment="Center">项目1</TextBlock>
                        <Button Content="OK" Width="80" HorizontalAlignment="Center"/>
                    </StackPanel>
                </Border>
            </ListBoxItem>
            <ListBoxItem>
                <Border>
                    <StackPanel>
                        <TextBlock HorizontalAlignment="Center">项目1</TextBlock>
                        <Button Content="OK" Width="80" HorizontalAlignment="Center"/>
                    </StackPanel>
                </Border>
            </ListBoxItem>
            <ListBoxItem>
                <Border>
                    <StackPanel>
                        <TextBlock HorizontalAlignment="Center">项目1</TextBlock>
                        <Button Content="OK" Width="80" HorizontalAlignment="Center"/>
                    </StackPanel>
                </Border>
            </ListBoxItem>
            <ListBoxItem>
                <Border>
                    <StackPanel>
                        <TextBlock HorizontalAlignment="Center">项目1</TextBlock>
                        <Button Content="OK" Width="80" HorizontalAlignment="Center"/>
                    </StackPanel>
                </Border>
            </ListBoxItem>
        </ListBox>
    </Grid>
</Window>

2 样式文件中代码

<!--设置ListBox样式-->

<Style TargetType="ListBox" x:Key="ListBoxHor">

    <!--设置模板-->
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ListBox">
                    <ScrollViewer HorizontalScrollBarVisibility="Disabled" VerticalScrollBarVisibility="Auto">
                        <WrapPanel Orientation="Horizontal" IsItemsHost="True" ScrollViewer.CanContentScroll="True"/>
                    </ScrollViewer>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

<!--设置ListBoxItem样式-->
    <Style TargetType="ListBoxItem">
        <Setter Property="Width" Value="120"></Setter>
        <Setter Property="Height" Value="40"></Setter>
        <Setter Property="Margin" Value="5"></Setter>
        <Setter Property="BorderBrush" Value="Red"/>
        <Setter Property="BorderThickness" Value="1"/>
        <!--设置控件模板-->
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ListBoxItem">
                    <Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}">
                        <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" TextBlock.Foreground="{TemplateBinding Foreground}"/>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <!--设置触发器-->
        <Style.Triggers>
            <Trigger Property="IsSelected" Value="true">
                <Setter Property="Background" Value="#808080"/>
                <Setter Property="Foreground" Value="White"/>
                <Setter Property="BorderBrush" Value="Green"/>
                <Setter Property="BorderThickness" Value="2"/>
            </Trigger>
            <Trigger Property="IsMouseOver" Value="true">
                <Setter Property="Background" Value="Red"/>
                <Setter Property="BorderBrush" Value="Black"/>
                <Setter Property="BorderThickness" Value="2"/>
            </Trigger>
        </Style.Triggers>
    </Style>

WPF中ListBox的样式设置的更多相关文章

  1. WPF中ListBox滚动时的缓动效果

    原文:WPF中ListBox滚动时的缓动效果 上周工作中遇到的问题: 常规的ListBox在滚动时总是一格格的移动,感觉上很生硬. 所以想要实现类似Flash中的那种缓动的效果,使ListBox滚动时 ...

  2. WPF中ListBox的项ListBoxItem被选中的时候Background变化

    使用WPF 中ListBox,点击ListBoxItem的时候,自定义它的背景色,曾经在网上找了一些方法, 不是很理想,后来在StackOverflow上找到了,贴出代码和效果图: 效果图:

  3. 转:WPF中ListBox的创建和多种绑定用法

    先从最容易的开始演示ListBox控件的创建. Adding ListBox Items下面的代码是向ListBox控件中添加多项ListBoxItem集合.XAML代码如下:<ListBox ...

  4. WPF中ListBox的绑定

    WPF中列表式控件派生自ItemsControl类,继承了ItemsSource属性.ItemsSource属性可以接收一个IEnumerable接口派生类的实例作为自己的值(所有可被迭代遍历的集合都 ...

  5. WPF 中的style 样式

    WPF相较于以前学的WinForm,WPF在UI设计与动画方面的炫丽是最吸引我来学习的.在WPF中XMAL代码的引入使得代码的编写能够前后端分离,为获得更好的界面,也使得我们不得不分出一半的时间花在前 ...

  6. WPF中未将对象引用设置到对象的实例

    前几天,我开始了WPF的基础学习,一上来我就遇到了一个令我头痛的问题,按照书上的例子我写了一段属于自己的代码,一个简单的色调器.满心期待的编译运行,就出现了未将对象引用设置到对象的实例.我在网上查阅了 ...

  7. WPF中ListBox /ListView如何改变选中条背景颜色

    适用ListBox /ListView WPF中LISTVIEW如何改变选中条背景颜色 https://www.cnblogs.com/sjqq/p/7828119.html

  8. WPF中ListBox ListView数据翻页浏览笔记(强调:是数据翻页,非翻页动画)

    ListBox和ListView在应用中,常常有需求关于每页显示固定数量的数据,然后通过Timer自动或者手动翻页操作,本文介绍到的就是该动作的实现. 一.重点 对于ListBox和ListView来 ...

  9. WPF教程十:如何使用Style和Behavior在WPF中规范视觉样式

    在使用WPF编写客户端代码时,我们会在VM下解耦业务逻辑,而剩下与功能无关的内容比如动画.视觉效果,布局切换等等在数量和复杂性上都超过了业务代码.而如何更好的简化这些编码,WPF设计人员使用了Styl ...

随机推荐

  1. mac idea 设置

    鼠标悬停显示文档注释:preferences->Editor->General:勾选 show quick documentation on mouse move 智能提示模糊搜索:pre ...

  2. mysql--1130ERROR

    问题一:mysql 用户登录不用验证密码(用户已设密码) 安装好mysql 后,我发现我设立了密码,同样可以不用密码就能登陆 cmd>mysql cmd>select current_us ...

  3. 异步数据库查询 Z

    Introduction Microsoft .NET 4.5 introduced new "async and await" methods to provide an eas ...

  4. src与href属性的区别

    src和href之间存在区别,能混淆使用.src用于替换当前元素,href用于在当前文档和引用资源之间确立联系. src是source的缩写,指向外部资源的位置,指向的内容将会嵌入到文档中当前标签所在 ...

  5. WANem2.3

    http://downloads.sourceforge.net/wanem http://openmaniak.com/wanem_network.php 只能以iso方式运行,安装到硬盘后无法保存 ...

  6. (LinkedList)Intersection of Two Linked Lists

    Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...

  7. Android 常见问题收集 (持续更新)

    gridview中设置item的高度. 一.BaseAdapter中的getView中设置 AbsListView.LayoutParams param = new AbsListView.Layou ...

  8. vs无法调试的时候

    ①选中所有程序可能经过的地方设置断点 ②清空.net frameword对应的缓存,路径"C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temp ...

  9. 【转】JVM 堆内存设置原理

    堆内存设置 原理 JVM堆内存分为2块:Permanent Space 和 Heap Space. Permanent 即 持久代(Permanent Generation),主要存放的是Java类定 ...

  10. web项目启动报错Unknown character set: 'utf8mb4' in mysql

    网上一查,有的说是mysql驱动的问题,有的说创建数据库的时候指定utf8编码,换了各种mysql版本,最后换了5.1.6版本的mysql驱动后成功启动!问题解决!OMG