- Java锁的逻辑(结合对象头和ObjectMonitor)
- 还在用饼状图?来瞧瞧这些炫酷的百分比可视化新图形(附代码实现)⛵
- 自动注册实体类到EntityFrameworkCore上下文,并适配ABP及ABPVNext
- 基于Sklearn机器学习代码实战
WPF入门教程系列三——Application介绍(续) 。
WPF入门教程系列五——Window 介绍 。
。
5、DataGrid的DataGridComboBoxColumn列的绑定方式 。
在上一篇文章的示例中,存在一个问题,在点击“刷新”按钮之后,城市这个ComboBox列的数据没有显示.
DataGridComboBoxColumn列如果要填充数据,首先要设置列的ItemsSouce属性,而且这个属性对于要绑定的数据源有以下的要求:
1. 在使用DataGrid的时候,有时候需要使某些列为ComboBox,这时自然想到使用DataGridComboBoxColumn,但是如果使用的是ItemsSource数据绑定后台的对象,就会发现,这根本就不能用.
2.在Visual Studio 2022中打开MainWindow.cs文件,添加下拉框的绑定代码。具体代码如下:
private
void
BindDrp() { cboCity.ItemsSource
=
GetCitys(); }
private
void
btnRefresh_Click(
object
sender, RoutedEventArgs e) { BindGrid(); BindDrp(); }
3. 在Visual Studio 2022中打开MainWindow.xaml文件,对DataGridComboBoxColumn进行了数据绑定。具体代码如下.
<
DataGridComboBoxColumn
Header
="城市"
Width
="120"
x:Name
="cboCity"
ClipboardContentBinding
="
{x:Null}
"
SelectedValuePath
="Code"
SelectedValueBinding
="
{Binding Path=CityCode,UpdateSourceTrigger=PropertyChanged}
"
DisplayMemberPath
="Name"
SelectedItemBinding
="
{x:Null}
"
/>
其中SelectedValuePath与DisplayMemberPath是指将绑定到DataGridComboBoxColumn列上的类中的哪个属性。例如上面的代码,code属性做为value值,Name属性做为在界面上呈现.
通过SelectedValueBinding绑定的值,这个值由SelectedValuePath 所绑定的属性确定,呈现绑定对象上的属性.
例如上述代码中,将SelectedValueBinding绑定到CityCode,当属性绑定发生变化时。通过SelectedValuePath=Code,表示我们通过Code这个关键字进行搜索,搜索绑定对象的Code值是否与CityCode值相同,相同返回City对象,而City对象中有Code属性和Name属性,并以Name属性进行显示.
4. 在Visual Studio 2022中按F5键,运行WFP应用程序,使用鼠标左键,点击“刷新”按钮,DataGrid中的城市列默认显示正确的绑定数据,如下图.
。
5. 下面是全部完成之后的实际的XAML代码.
<
Window
x:Class
="WpfGridDemo.NET7.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:WpfGridDemo.NET7"
mc:Ignorable
="d"
Title
="MainWindow"
Height
="600"
Width
="960"
>
<
Grid
>
<
Grid.RowDefinitions
>
<
RowDefinition
Height
="100"
></
RowDefinition
>
<
RowDefinition
Height
="*"
></
RowDefinition
>
<
RowDefinition
Height
="25"
></
RowDefinition
>
</
Grid.RowDefinitions
>
<
DataGrid
x:Name
="gridArea"
Grid.Row
="1"
d:ItemsSource
="
{d:SampleData ItemCount=5}
"
AutoGenerateColumns
="False"
HorizontalAlignment
="Left"
VerticalAlignment
="Top"
>
<
DataGrid.Columns
>
<
DataGridComboBoxColumn
Header
="城市"
Width
="120"
x:Name
="cboCity"
ClipboardContentBinding
="
{x:Null}
"
SelectedValuePath
="Code"
SelectedValueBinding
="
{Binding Path=CityCode,UpdateSourceTrigger=PropertyChanged}
"
DisplayMemberPath
="Name"
SelectedItemBinding
="
{x:Null}
"
/>
<
DataGridTextColumn
Header
="县区镇"
Width
="*"
Binding
="
{Binding Name}
"
ClipboardContentBinding
="
{x:Null}
"
/>
<
DataGridTextColumn
Header
="邮编"
Width
="100"
Binding
="
{Binding Code}
"
ClipboardContentBinding
="
{x:Null}
"
/>
<
DataGridTextColumn
Header
="创建时间"
Width
="160"
Binding
="
{Binding Created}
"
ClipboardContentBinding
="
{x:Null}
"
/>
<
DataGridTextColumn
Header
="更新时间"
Width
="160"
Binding
="
{Binding Updated}
"
ClipboardContentBinding
="
{x:Null}
"
/>
</
DataGrid.Columns
>
</
DataGrid
>
<
WrapPanel
Grid.Row
="2"
>
<
Button
x:Name
="btnRefresh"
Height
="22"
Width
="120"
Click
="btnRefresh_Click"
>
刷新
</
Button
>
<
Button
x:Name
="btnSave"
Height
="22"
Width
="120"
>
保存
</
Button
>
</
WrapPanel
>
</
Grid
>
</
Window
>
6.MainWidow.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
WpfGridDemo.NET7.Entitys;
namespace
WpfGridDemo.NET7 {
///
<summary>
///
Interaction logic for MainWindow.xaml
///
</summary>
public
partial
class
MainWindow : Window {
public
MainWindow() { InitializeComponent(); } GridDbContext db
=
new
GridDbContext();
protected
List<City>
GetCitys() { List
<City> list = db.City.ToList<City>
();
return
list; }
protected
List<Area>
GetAreas() { List
<Area> list = db.Area.ToList<Area>
();
return
list; }
protected
List<Province>
GetProvinces() { List
<Province> list = db.Province.ToList<Province>
();
return
list; }
private
void
BindGrid() { gridArea.ItemsSource
=
GetAreas(); }
private
void
BindDrp() { cboCity.ItemsSource
=
GetCitys(); }
private
void
btnRefresh_Click(
object
sender, RoutedEventArgs e) { BindGrid(); BindDrp(); } } }
。
最后此篇关于WPF入门教程系列二十六——DataGrid使用示例(3)的文章就讲到这里了,如果你想了解更多关于WPF入门教程系列二十六——DataGrid使用示例(3)的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
Hive —— 入门 Hive介绍 Apache Hive是一款建立在Hadoop之上的开源数据仓库系统,可以将存储在Hadoop文件中的结构化、半结构化数据文件映射为一张数据库表,基于表提供了一
HBase —— 入门 HBase介绍 HBase是一个分布式的、面向列的开源数据库,该技术来源于 Fay Chang 所撰写的Google论文“Bigtable:一个结构化数据的分布式存储系统”
零:前端目前形势 前端的发展史 HTML(5)、CSS(3)、JavaScript(ES5、ES6):编写一个个的页面 -> 给后端(PHP、Python、Go、Java) ->
在本教程中,您将了解在计算机上运行 JavaScript 的不同方法。 JavaScript 是一种流行的编程语言,具有广泛的应用程序。 JavaScript 以前主要用于使网页具有交
我曾经是一个对编程一窍不通的小白,但因为对互联网世界的好奇心和求知欲的驱使,我踏入了编程的殿堂。在学习的过程中,我发现了一门神奇的编程语言——Python。Python有着简洁、易读的语法,让初学者能
嗨,亲爱的读者们! 今天我要给大家分享一些关于Python爬虫的小案例。你是否曾为了获取特定网页上的数据而烦恼过?或者是否好奇如何从网页中提取信息以供自己使用?那么,这篇文章将会给你一些启示和灵感。
关闭。这个问题是opinion-based 。目前不接受答案。 想要改进这个问题吗?更新问题,以便 editing this post 可以用事实和引文来回答它。 . 已关闭 8 年前。 Improv
我想创建一个像https://apprtc.appspot.com/?r=04188292这样的应用程序。我对 webrtc 了解一点,但无法掌握 google app-engine。如何为 java
我刚刚开始使用 Python 并编写了一个简单的周边程序。但是,每当我在终端中键入 python perimeter.py 时,都会收到以下错误,我不知道如何解决。 >>> python perime
Redis有5个基本数据结构,string、list、hash、set和zset。它们是日常开发中使用频率非常高应用最为广泛的数据结构,把这5个数据结构都吃透了,你就掌握了Redis应用知识的一半了
创建发布web项目 具体步骤: 1.在开发工具中创建一个dynamic web project helloword 2.在webContent中创建index.html文件 3.发布web应用到
如果你在 Ubuntu 上使用终端的时间很长,你可能会希望调整终端的字体和大小以获取一种良好的体验。 更改字体是一种最简单但最直观的 Linux 的终端自定义 的方法。让我
1. 前言 ADODB 是 Active Data Objects Data Base 的简称,它是一种 PHP 存取数据库的函式组件。现在 SFS3 系统 (校园自由软件交流网学务系统) 计划的
我对 neo4j 完全陌生,我很抱歉提出这样一个基本问题。我已经安装了neo4j,我正在使用shell“localhost:7474/webadmin/#/console/” 我正在寻找一个很好的例子
我正在阅读 ios 4 的核心音频,目的是构建一个小测试应用程序。 在这一点上,我对所有 api 的研究感到非常困惑。理想情况下,我想知道如何从两个 mp3 中提取一些样本到数组中。 然后在回调循环中
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是无关紧要的,因
我下载了 GNUStep并安装了它,但是我不确定在哪里可以找到 IDE。有谁知道什么程序可以用作 GNUStep IDE/从哪里获取它们?否则,有没有人知道有关如何创建和编译基本 GNUStep 程序
我正在尝试开始使用 Apache Solr,但有些事情我不清楚。通读tutorial ,我已经设置了一个正在运行的 Solr 实例。我感到困惑的是 Solr 的所有配置(架构等)都是 XML 格式的。
请问有没有关于如何开始使用 BruTile 的文档? 我目前正在使用 SharpMap,我需要预缓存切片以加快进程 最佳答案 我今天正在研究这个:)Mapsui项目site严重依赖 SharpMap
尽我所能,我无法让 CEDET 做任何事情。 Emacs 24.3。我下载了最新的 CEDET 快照。我从他的底部(不是这样)Gentle Introduction 中获取了 Alex Ott 的设置
我是一名优秀的程序员,十分优秀!