WPF學習(4) -- 數據模板

一、DataTemplate

在WPF(Windows Presentation Foundation)中,DataTemplate 用于定義數據的可視化呈現方式。它允許你自定義如何展示數據對象,從而實現更靈活和豐富的用戶界面。DataTemplate 通常用于控件(如ListBoxComboBoxDataGrid等)的項模板。

1.代碼示例

1.1 xaml.cs

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;
using System.Collections.ObjectModel;namespace 學習
{public partial class MainWindow : Window{public MainWindow(){InitializeComponent();List<Color> test = new List<Color>();test.Add(new Color() { Code = "Red", Name = "紅色" });test.Add(new Color() { Code = "BLUE", Name = "藍色" });test.Add(new Color() { Code = "YELLOW", Name = "黃色" });test.Add(new Color() { Code = "GREEN", Name = "綠色" });list.ItemsSource = test;}}public class Color{public string Code { get; set; }public string Name { get; set; }}
}

1.2 xaml

<Window x:Class="學習.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:學習"mc:Ignorable="d"Title="MainWindow" Height="450" Width="800"><Grid><ListBox x:Name="list"><ListBox.ItemTemplate><DataTemplate><StackPanel Orientation="Horizontal"><Border Width="10" Height="10"Background="{Binding Code}"></Border><TextBlock Margin="10,0" Text="{Binding Name}"></TextBlock></StackPanel></DataTemplate></ListBox.ItemTemplate></ListBox></Grid>
</Window>

2.代碼結果

3.代碼示例2

后端不變

<Window x:Class="學習.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:學習"mc:Ignorable="d"Title="MainWindow" Height="450" Width="800"><Grid><DataGridx:Name="list"AutoGenerateColumns="False"CanUserAddRows="False"><DataGrid.Columns><DataGridTextColumn Binding="{Binding Name}" Header="Name"/><DataGridTextColumn Binding="{Binding Code}" Header="Code"/><DataGridTemplateColumn Header="操作"><!--可操作的--><DataGridTemplateColumn.CellTemplate><DataTemplate><StackPanel Orientation="Horizontal"><Border Width="10"Height="10"Background="{Binding Code}"></Border><TextBlock Margin="10" Text="{Binding Name}"></TextBlock></StackPanel></DataTemplate></DataGridTemplateColumn.CellTemplate></DataGridTemplateColumn></DataGrid.Columns></DataGrid></Grid>
</Window>

4.代碼結果

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/web/45208.shtml
繁體地址,請注明出處:http://hk.pswp.cn/web/45208.shtml
英文地址,請注明出處:http://en.pswp.cn/web/45208.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

知識圖譜和 LLM:利用 Neo4j 實現大型語言模型

這是關于 Neo4j 的 NaLLM 項目的一篇博客文章。這個項目是為了探索、開發和展示這些 LLM 與 Neo4j 結合的實際用途。 2023 年,ChatGPT 等大型語言模型 (LLM) 因其理解和生成類似人類的文本的能力而風靡全球。它們能夠適應不同的對話環境、回答各種主題的問題,甚至模擬創意寫…

NSSCTF中24網安培訓day1中web的題目

我flag呢 直接查看源代碼即可CtrlU [SWPUCTF 2021 新生賽]Do_you_know_http 用Burpsuite抓包&#xff0c;之后在User-agent下面添加XFF頭&#xff0c;即X-Forwarded-For:127.0.0.1 [SWPUCTF 2022 新生賽]funny_php 首先是php的弱比較&#xff0c;對于num參數&#xff0c;我們…

hot100 | 十一、二分搜索

1-leetcode35. 搜索插入位置 注意&#xff1a; 看Labuladong的書&#xff0c;知道while的判斷符號跟left right的關系 public int searchInsert(int[] nums, int target) {int left 0;int right nums.length - 1;while (left < right) {int mid left (right - left) /…

AI如何引領個人潛力的深度挖掘

AI如何引領個人潛力的深度挖掘 人工智能&#xff08;AI&#xff09;不僅是一場技術革命&#xff0c;更是對人類自身能力的一次深刻反思。本文旨在探討在AI時代下&#xff0c;個人如何挖掘并發揮自己的最大潛能&#xff0c;不僅在職場、教育領域找到新的定位&#xff0c;同時也…

PostgreSQL日志文件配置,記錄所有操作記錄

為了更詳細的記錄PostgreSQL 的運行日志&#xff0c;我們一般需要修改PostgreSQL 默認的配置文件&#xff0c;這里整理了一些常用的配置 修改配置文件 打開 PostgreSQL 配置文件 postgresql.conf。該文件通常位于 PostgreSQL 安裝目錄下的 data 文件夾中。 找到并修改以下配…

Python循環遍歷:深入理解與實戰應用

在Python編程中&#xff0c;循環遍歷是一種基本且強大的控制流結構&#xff0c;它允許我們重復執行一段代碼直到滿足某個條件為止。無論是處理數據集合&#xff08;如列表、元組、字典、集合等&#xff09;&#xff0c;還是執行重復的任務&#xff0c;循環遍歷都是不可或缺的工…

807.保持城市天際線

解題思路 首先找到四個主要方向&#xff08;東南西北&#xff09;的天際線情況。南北看是一樣的&#xff0c;東西看也是一樣的。所以統計出每行的最值&#xff0c;每列的最值&#xff0c;用一個n的數組存儲。分別存儲行和列的最值。最值的位置進行標記&#xff0c;然后對于其余…

【Qt 基礎】繪圖

畫筆 QPen pen; pen.setWidth(3); // 線條寬度 pen.setColor(Qt::red);// 畫筆顏色 pen.setStyle(Qt::DashLine);// 線條樣式 pen.setCapStyle(Qt::RoundCap);// 線端樣式 pen.setJoinStyle(Qt::BevelJoin);// 連接樣式 painter.setPen(pen);線條 線端 連接 畫刷 QBrush bru…

Spring容器詳細介紹

Spring容器 1 Spring核心容器介紹 問題導入 問題&#xff1a;按照Bean名稱獲取Bean有什么弊端&#xff0c;按照Bean類型獲取Bean有什么弊端&#xff1f; 1.1 創建容器 方式一&#xff1a;類路徑加載配置文件 ApplicationContext ctx new ClassPathXmlApplicationContext…

復合類型的字節對齊

引子 #inlcude<stdio.h> struct s{int i;char a: }; struct s sVar {5,A}; int main(void){printf("%d\n",sizeof(sVar)); }問1&#xff1a;上面這個代碼的輸出結果是多少&#xff1f; 答1&#xff1a; 思考 明明sVar這個結構體就兩個元素&#xff0c;5和…

什么是冪等?如何實現冪等?

一 定義 冪等性&#xff08;Idempotence&#xff09;是數學與計算機科學中的一個概念&#xff0c;它指的是一個操作、函數或方法被重復執行多次與僅執行一次的效果相同&#xff0c;或者說&#xff0c;其后續調用的結果不會改變之前調用的結果。 在計算機科學中&#xff0c;這個…

Spring Boot實戰:無縫對接OpenAI

Spring Boot實戰&#xff1a;無縫對接OpenAI 在當今的技術領域&#xff0c;人工智能&#xff08;AI&#xff09;已經成為一股不可忽視的力量。OpenAI作為其中的佼佼者&#xff0c;提供了強大的API供開發者使用&#xff0c;以實現各種AI功能。本文將詳細介紹如何使用Spring Boo…

開閉原則 (Open/Closed Principle, OCP)

開閉原則 (Open/Closed Principle, OCP) 開閉原則&#xff08;Open/Closed Principle, OCP&#xff09;是面向對象設計的五大原則之一。它的基本思想是&#xff1a;軟件實體&#xff08;類、模塊、函數等&#xff09;應該對擴展開放&#xff0c;對修改關閉。即在不修改現有代碼…

uniapp實現水印相機

uniapp實現水印相機-livePusher 水印相機 背景 前兩天拿到了一個需求&#xff0c;要求在內部的oaApp中增加一個衛生檢查模塊&#xff0c;這個模塊中的核心訴求就是要求拍照的照片添加水印。對于這個需求&#xff0c;我首先想到的是直接去插件市場&#xff0c;下一個水印相機…

多頭注意力機制詳解:多維度的深度學習利器

引言 多頭注意力機制是對基礎注意力機制的一種擴展&#xff0c;通過引入多個注意力頭&#xff0c;每個頭獨立計算注意力&#xff0c;然后將結果拼接在一起進行線性變換。本文將詳細介紹多頭注意力機制的原理、應用以及具體實現。 原理 多頭注意力機制的核心思想是通過多個注…

springAMQP自定義fanout交換機進行消息的廣播

rabbitmq一共有三種交換機&#xff1a; fanout--廣播direct--定向topic--話題 rabbitmq-web端 首先我們需要建立一個名叫cybg.fanout交換機與兩個自定義的隊列用于測試廣播效果 我這里就起名字叫做fanout_queue1&fanout_queue2 項目中&#xff1a; 首先對我們的Liste…

當代政治制度(練習題)

當代政治制度&#xff08;練習題&#xff09; *** Rz整理 僅供參考 *** 目前地方人大設立的專門委員會不包括&#xff08;B.法律審查委員會F.外交事務專門委員會 &#xff09;答案不確定 等待指點 A.法制委員會 B.法律審查委員會 C.財政經濟委員會 D.社會建設委員會 E.農業與…

Go語言基礎數據類型、變量及自增語法

本文內容為Go語言的基礎數據類型、變量定義和賦值及自增語法介紹。 目錄 基礎數據類型 變量 先定義后賦值 定義時直接賦值 自動推導定義賦值 平行賦值 自增語法 總結 基礎數據類型 int,int8 intl6, int32, int64 uint8... uint64 float32,float64 true/false 變量 …

unity 環形循環切換UI

環形ui管理器 using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using DG.Tweening; using System.Collections; using Unity.VisualScripting;public class LevelSelector : MonoBehaviour {public GameObject levelButtonPrefab; // 關卡按鈕的…

Elasticsearch:介紹 retrievers - 搜索一切事物

作者&#xff1a;來自 Elastic Jeff Vestal, Jack Conradson 在 8.14 中&#xff0c;Elastic 在 Elasticsearch 中引入了一項名為 “retrievers - 檢索器” 的新搜索功能。繼續閱讀以了解它們的簡單性和效率&#xff0c;以及它們如何增強你的搜索操作。 檢索器是 Elasticsearc…