首页 > 编程语言 >C# ControlTemplate 和 DataTemplate 一起使用

C# ControlTemplate 和 DataTemplate 一起使用

时间:2024-08-18 11:15:32浏览次数:4  
标签:Abc C# Price System Windows ControlTemplate new using 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; }
}    
}

 

标签:Abc,C#,Price,System,Windows,ControlTemplate,new,using,DataTemplate
From: https://www.cnblogs.com/sound-of-wind-rain/p/18365410

相关文章

  • 题解:AT_abc367_e [ABC367E] Permute K times
    题意给你一个长度为\(N\)的序列\(X\),其中每个元素都介于\(1\)和\(N\)之间(含),以及一个长度为\(N\)的序列\(A\)。打印在\(A\)上执行以下操作\(K\)次的结果。将\(A_i\)替换为\(A_{X_i}\)。每个操作同时进行。思路元素\(i\)经过\(k\)次变化后的值就是元素......
  • leetcode 21.合并两个有序链表
    leetcode21.合并两个有序链表题目描述:将两个升序链表合并为一个新的升序链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。迭代法:思路:不断迭代,谁小指向谁publicListNodemergeTwoLists(ListNodelist1,ListNodelist2){if(list1==null){......
  • leetcode3.无重复字符的最长子串
    题目:给定一个字符串s,请你找出其中不含有重复字符的最长子串的长度。思路:滑动窗口算法。最大子串长度=右下标-左下标+1;使用map记录字符的下标,key为字符,value为下标。如果map的key出现重复,说明有重复字符了。找出左下标left,如果重复就刷新左下标。代码:publ......
  • [C++ Error] f0201.cpp(11): E2379 Statement missing ;
    错误解释:这个错误表明在C++源代码文件f0201.cpp的第11行出现了一个语法错误,具体是缺少了一个分号;。C++语言规定语句的结束需要使用分号;,如果一个语句缺少了它,编译器就会抛出这样的错误。解决方法:打开f0201.cpp文件``,定位到第11行。检查那一行的代码,确保每个语句后面都有分号;......
  • lit parcel模板
    mkdirmyproject/src-pcdmyprojectpnpmilittypescripttsconfig.json:{"compilerOptions":{"target":"ES2020","experimentalDecorators":true,"useDefineForClassFields":......
  • OpenCV 高斯模糊
    高斯分布:高斯模糊的原理一:图像产生高斯噪声循环代码实现(耗时)defclamp(pv):#使我们的随机值在0-255之间ifpv>255:return255ifpv<0:return0returnpvimportcv2ascvimportnumpyasnpdefgaussian_noise(image):......
  • OpenCV 模糊操作
    模糊操作三种模糊操作方式均值模糊中值模糊自定义模糊(可以实现上面两种模糊方式)原理:图像处理:基础(模板、卷积运算)图像处理-模板、卷积的整理基于离散卷积定义好每个卷积核不同卷积核得到不同的卷积效果模糊是卷积的一种表象一:均值模糊blurdefblur_demo(imag......
  • OpenCV 图像直方图
    一:直方图的直接使用frommatplotlibimportpyplotaspltdefplot_demo(image):print(image.ravel())plt.hist(image.ravel(),256,[0,256])#ravel将图像3维转一维数组,便于统计频率#统计为256个bin,显示0-256bin,意思是全部显示,我们可以设置只显示一部分plt.show()......
  • OpenCV 边缘保留滤波EPF
    OpenCV经典的两种实现EPF方法:高斯双边和均值迁移一:双边模糊差异越大,越会完整保留defbi_demo(image):dst=cv.bilateralFilter(image,0,100,15)#第二个参数d是distinct,我们若是输入了d,会根据其去算第3或4个参数,我们最好是使用第3或4个参数反算d,先设为0cv......
  • Cyber Triage 3.10 (Windows) - 数字取证和事件响应
    CyberTriage3.10(Windows)-数字取证和事件响应DigitalForensicsSpecializedForIncidentResponse请访问原文链接:https://sysin.org/blog/cybertriage-3/,查看最新版。原创作品,转载请保留出处。唯一专门用于事件响应的数字取证工具快速、准确和简单地完成入侵调......