WPF datagrid 初学
<Window x:Class="WpfDemo.WinDataGrid"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:assembly="clr-namespace:System;assembly=mscorlib"
xmlns:local="clr-namespace:WpfDemo"
Title="WinDataGrid" Height="591" Width="487" Loaded="Window_Loaded_1">
<Window.Resources>
<ObjectDataProvider x:Key="keySex" MethodName="GetValues" ObjectType="{x:Type assembly:Enum}">
<ObjectDataProvider.MethodParameters>
<x:Type Type="local:OrderStatus"></x:Type>
<!--引用后台的枚举类型,为字段‘性别’提供数据源。上面引用了命名空间Demo-->
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
</Window.Resources> <Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="37*"/>
<ColumnDefinition Width="60*"/>
</Grid.ColumnDefinitions>
<DataGrid x:Name="datagrid" AutoGenerateColumns="False" Margin="0,44,0,0" CanUserAddRows="False" LoadingRow="datagrid_LoadingRow" Grid.ColumnSpan="2" FrozenColumnCount="1">
<DataGrid.RowDetailsTemplate>
<DataTemplate>
<Border Margin="10" Padding="10" BorderBrush="SteelBlue" BorderThickness="3" CornerRadius="5">
<TextBlock Text="{Binding Path=Rack}" TextWrapping="Wrap"></TextBlock>
</Border>
</DataTemplate>
</DataGrid.RowDetailsTemplate>
<DataGrid.Columns>
<DataGridTextColumn Header="商品编号" Width="60" Binding="{Binding Path=Number}" ></DataGridTextColumn>
<DataGridTextColumn Header="商品名称" Width="90" Binding="{Binding Path=NAME}">
</DataGridTextColumn>
<DataGridTextColumn Header="价格" Width="80" Binding="{Binding Path=UnitCost, StringFormat={}{0:C}}"></DataGridTextColumn>
<DataGridTextColumn Header="简介" Width="175" Binding="{Binding Path=Description}"></DataGridTextColumn>
<DataGridCheckBoxColumn Header="是否售罄" Width="80" Binding="{ Binding Path=isSoldOut}"></DataGridCheckBoxColumn>
<DataGridTemplateColumn Header="状态">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Status}"/>
<!--显示状态时显示 TextBlock里的值-->
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<!--编辑状态就切换到ComboBox里进行下拉选择操作-->
<ComboBox x:Name="taskCombo" ItemsSource="{Binding Source={StaticResource keySex}}" SelectedItem ="{Binding Path=Status}" IsSynchronizedWithCurrentItem="False"/>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="日期">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Margin="4" Text="{Binding Path=AddDay,StringFormat={}{0:yyyy-MM-dd}}"></TextBlock>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<DatePicker SelectedDate="{Binding Path=AddDay,Mode=TwoWay}">
</DatePicker>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid> </Grid>
</Window>
后台代码很简单。。
using System;
using System.Collections.Generic;
using System.Data;
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;
using WpfDemo.Class;
namespace WpfDemo
{
/// <summary>
/// WinDataGrid.xaml 的交互逻辑
/// </summary>
public partial class WinDataGrid : Window
{
DataBind db = new DataBind();
private SolidColorBrush heiglihtbrush = new SolidColorBrush(Colors.Orange);
private SolidColorBrush normalbrush = new SolidColorBrush(Colors.White);
public WinDataGrid()
{
InitializeComponent();
} private void Window_Loaded_1(object sender, RoutedEventArgs e)
{
datagrid.ItemsSource = db.GetProduct().DefaultView; }
private void datagrid_LoadingRow(object sender, DataGridRowEventArgs e)
{
int i=e.Row.GetIndex();
DataTable dt = db.GetProduct();
if (int.Parse(dt.Rows[i]["unitcost"].ToString()) > )
{
e.Row.Background = heiglihtbrush;
}
else
{
e.Row.Background = normalbrush;
}
}
} }
namespace WpfDemo
{
public enum OrderStatus
{
None,
New,
Processing,
Shipped,
Received
}
}
做winfrom多了,不习惯wpf的绑定的方式。相信用的多了就好啦。理论上的东西,网上一搜一大片。就不复述了。这里只是一个简单的例子。
sql表如下。。没具体涵义。。就是为了实现例子拼凑的。

WPF datagrid 初学的更多相关文章
- WPF DataGrid常用属性记录
		WPF DataGrid常用属性记录 组件常用方法: BeginEdit:使DataGrid进入编辑状态. CancelEdit:取消DataGrid的编辑状态. CollapseRowGroup:闭 ... 
- WPF DATAGRID - COMMITTING CHANGES CELL-BY-CELL
		In my recent codeproject article on the DataGrid I described a number of techniques for handling the ... 
- WPF DataGrid某列使用多绑定后该列排序失效,列上加入 SortMemberPath 设置即可.
		WPF DataGrid某列使用多绑定后该列排序失效 2011-07-14 10:59hdongq | 浏览 1031 次 悬赏:20 在wpf的datagrid中某一列使用了多绑定,但是该列排序失 ... 
- xceed wpf datagrid
		<!--*********************************************************************************** Extended ... 
- 获取wpf datagrid当前被编辑单元格的内容
		原文 获取wpf datagrid当前被编辑单元格的内容 确认修改单元个的值, 使用到datagrid的两个事件 开始编辑事件 BeginningEdit="dataGrid_Beginni ... 
- WPF DataGrid绑定一个组合列
		WPF DataGrid绑定一个组合列 前台: <Page.Resources> <local:InfoConverter x:Key="converter& ... 
- WPF DataGrid自定义样式
		微软的WPF DataGrid中有很多的属性和样式,你可以调整,以寻找合适的(如果你是一名设计师).下面,找到我的小抄造型的网格.它不是100%全面,但它可以让你走得很远,有一些非常有用的技巧和陷阱. ... 
- WPF DataGrid显格式
		Guide to WPF DataGrid formatting using bindings Peter Huber SG, 25 Nov 2013 CPOL 4.83 (13 votes) ... 
- WPF DataGrid Custommization using Style and Template
		WPF DataGrid Custommization using Style and Template 代码下载:http://download.csdn.net/detail/wujicai/81 ... 
随机推荐
- ps -aux返回超过100%
			http://serverfault.com/questions/522922/cpu-more-than-100-in-ps-aux export NLS_LANG="SIMPLIFIED ... 
- Oracle数据导入导出imp/exp(转)
			在oracle安装目录下有EXP.EXE与IMP.EXE这2个文件,他们分别被用来执行数据库的导入导出.所以Oracle数据导入导出imp/exp就相当与oracle数据还原与备份. 一.Oracle ... 
- 【Lucene】挖掘相关搜索词
			搜索引擎中往往有一个可选的搜索词的列表,当搜索结果太少时,可以帮助用户扩展搜索内容,或者搜索结果太多的时候可以帮助用户深入定向搜索.一种方法是从搜索日志中挖掘字面相似的词作为相关搜索词列表.另一种方法 ... 
- 微信cookie内容
			#LWP-Cookies-1.0 Set-Cookie3: webwx_data_ticket="AQeVHpn/pdyrAQHCl++4ZvS0"; path="/&q ... 
- Java实现邮件代理发送
			使用java实现邮件发送 首先需要添加jar文件mailapi.jarstmp.jar 1 import java.util.Properties; import javax.mail.Address ... 
- 栈和队列的Java实现
			一. 栈 1.概念 栈是一种特殊的线性表,它只能在栈顶(top)进行插入(push)和删除(pop)操作. 栈的常用操作: 入栈(push):向栈顶插入元素 出栈(pop):从栈顶删除元素 访问栈顶 ... 
- 如果在文档已完成加载后执行 document.write,整个 HTML 页面将被覆盖
			<!DOCTYPE html> <html> <body> <h1>My First Web Page</h1> <p>My F ... 
- H264源码分析(四)
			sub_mb_pred( mb_type ) { for( mbPartIdx = 0; mbPartIdx < 4; mbPartIdx++ ) ... 
- KVC在定义Model类中的妙用
			@我们应用程序使用MVC架构的话,对于处理数据类,我们会单独的定义Model类,在里面为要展示的属性进行初始化赋值,一般採用的方法是通过定义相应的属性,挨个赋值.如今我要介绍的就是通过KVC,key- ... 
- 【玩转Ubuntu】02. Ubuntu上搭建Android开发环境
			一. 基本环境搭建 1.官网http://developer.android.com/sdk/index.html ,下载adt-bundle-linux-x86_64-20130729.zip 2. ... 
