C# ControlTemplate 和 DataTemplate 一起使用

复制代码
<Window x:Class="WpfApp1.Window2"
        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:WpfApp1"
        mc:Ignorable="d"
        Title="Window2" Height="450" Width="800">
    <Window.Resources>
        <Style x:Key="listBoxStyle" TargetType="{x:Type ListBox}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ListBox}">
                        <Border x:Name="Bd" Background="LightBlue" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Padding="1" SnapsToDevicePixels="true" CornerRadius="30">
                            <ScrollViewer Focusable="false" Padding="{TemplateBinding Padding}">
                                <ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                            </ScrollViewer>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsEnabled" Value="false">
                                <Setter Property="Background" TargetName="Bd" Value="{StaticResource ListBox.Disabled.Background}"/>
                                <Setter Property="BorderBrush" TargetName="Bd" Value="{StaticResource ListBox.Disabled.Border}"/>
                            </Trigger>
                            <MultiTrigger>
                                <MultiTrigger.Conditions>
                                    <Condition Property="IsGrouping" Value="true"/>
                                    <Condition Property="VirtualizingPanel.IsVirtualizingWhenGrouping" Value="false"/>
                                </MultiTrigger.Conditions>
                                <Setter Property="ScrollViewer.CanContentScroll" Value="false"/>
                            </MultiTrigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
            <Setter Property="ItemTemplate">
                <Setter.Value>
                    <DataTemplate >
                        <Grid>
                            <StackPanel Orientation="Horizontal">
                                <Grid>
                                    <Rectangle Stroke="Red" Fill="Orange" Width="{Binding Price}"/>
                                    <TextBlock Text="{Binding Year}"/>
                                </Grid>
                                <TextBlock Text="{Binding Price}" Margin="5,0"/>
                            </StackPanel>
                        </Grid>
                    </DataTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>
    <Grid>
        <ListBox x:Name="listBox1" Style="{StaticResource listBoxStyle}" Height="200" Width="300"/>
    </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.Shapes;

namespace WpfApp1
{
    /// <summary>
    /// Interaction logic for Window2.xaml
    /// </summary>
    public partial class Window2 : Window
    {
        public Window2()
        {
            InitializeComponent();

            List<Abc> list = new List<Abc>()
            {
                new Abc(){ Price=100, Year=1990},
                new Abc(){ Price=150, Year= 1991 },
                new Abc(){ Price=200, Year= 2000},
                new Abc(){ Price=80, Year= 1894 },
            };
            this.listBox1.ItemsSource = list;
        }
    }

public class Abc
{
    public int Price { get; set; }
    public int Year { get; set; }
}    
}
复制代码

 

posted @   竹楼风雨声  阅读(4)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
历史上的今天:
2022-08-18 SQL SERVER 2008 复制所有表结构、触发器、存储过程、视图等(海典传输初始化)(二)(对一中的语句进行解析或者替换)
2022-08-18 SQL SERVER 2008 复制所有表结构、触发器、存储过程、视图等(海典传输初始化)(一)
2022-08-18 SQL SERVER 附加数据库时报错(5123,5120):无法打开物理文件……操作系统错误,拒绝访问……
点击右上角即可分享
微信分享提示