<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 初学的更多相关文章

  1. WPF DataGrid常用属性记录

    WPF DataGrid常用属性记录 组件常用方法: BeginEdit:使DataGrid进入编辑状态. CancelEdit:取消DataGrid的编辑状态. CollapseRowGroup:闭 ...

  2. WPF DATAGRID - COMMITTING CHANGES CELL-BY-CELL

    In my recent codeproject article on the DataGrid I described a number of techniques for handling the ...

  3. WPF DataGrid某列使用多绑定后该列排序失效,列上加入 SortMemberPath 设置即可.

    WPF DataGrid某列使用多绑定后该列排序失效 2011-07-14 10:59hdongq | 浏览 1031 次  悬赏:20 在wpf的datagrid中某一列使用了多绑定,但是该列排序失 ...

  4. xceed wpf datagrid

    <!--*********************************************************************************** Extended ...

  5. 获取wpf datagrid当前被编辑单元格的内容

    原文 获取wpf datagrid当前被编辑单元格的内容 确认修改单元个的值, 使用到datagrid的两个事件 开始编辑事件 BeginningEdit="dataGrid_Beginni ...

  6. WPF DataGrid绑定一个组合列

    WPF DataGrid绑定一个组合列 前台: <Page.Resources>        <local:InfoConverter x:Key="converter& ...

  7. WPF DataGrid自定义样式

    微软的WPF DataGrid中有很多的属性和样式,你可以调整,以寻找合适的(如果你是一名设计师).下面,找到我的小抄造型的网格.它不是100%全面,但它可以让你走得很远,有一些非常有用的技巧和陷阱. ...

  8. WPF DataGrid显格式

    Guide to WPF DataGrid formatting using bindings Peter Huber SG, 25 Nov 2013 CPOL    4.83 (13 votes) ...

  9. WPF DataGrid Custommization using Style and Template

    WPF DataGrid Custommization using Style and Template 代码下载:http://download.csdn.net/detail/wujicai/81 ...

随机推荐

  1. 项目与软件推荐之编辑器-QOwnNotes(刺激自己)

    项目与软件推荐之编辑器-QOwnNotes 今天推荐一款软件 QOwnNotes,是一款普通文本笔记软件.以某个路径为目录,罗列出目录下所有的 md 文件或者 txt 文件. 有如下亮点: 启动速度快 ...

  2. draw lines on ColumnChart

    原文 http://blog.csdn.net/lixuekun820/article/details/5485042 Summary: Adobe 的 Flex Chart提供了很强大的功能,通过简 ...

  3. 轻松实现语音识别的完整代码在android开发中

    苹果的iphone 有语音识别用的是Google 的技术,做为Google 力推的Android 自然会将其核心技术往Android 系统里面植入,并结合google 的云端技术将其发扬光大. * C ...

  4. 多校联合练习赛1 Problem1005 Deque LIS+LDS 再加一系列优化

    Deque Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Subm ...

  5. Codeforces Round #258 (Div. 2/C)/Codeforces451C_Predict Outcome of the Game(枚举)

    解题报告 http://blog.csdn.net/juncoder/article/details/38102391 题意: n场比赛当中k场是没看过的,对于这k场比赛,a,b,c三队赢的场次的关系 ...

  6. C语言中一些非常酷的技巧(cool tricks)

    来自Quora,认为不错,就实践了一下. 1.  #if 0 ...... #endif 块中的内容不会被编译,由于凝视不同意嵌套,我们能够把临时不用的代码块放在 这里面. 2. 数组初始化的时候能够 ...

  7. ASP.NET Web API Authorization using Tokens

    Planning real world REST API http://blog.developers.ba/post/2012/03/03/ASPNET-Web-API-Authorization- ...

  8. linux 搭建Tomcat

    这几天用虚拟机装了centos ,以前没有用过linux,先将搭建java web 环境记录如下: 一.卸载系统安装的jdk环境. 1.安装好的CentOS会自带OpenJdk,用命令 java -v ...

  9. box-shadow属性

    一.定义和用法 box-shadow属性 向框添加一个或多个阴影. 二.语法 box-shadow: h-shadow v-shadow blur spread color inset; h-shad ...

  10. java.lang.IllegalStateException: Target host must not be null, or set in parameters. scheme=null, host=null, path=Aict/listPagedAict.action

    原因:请求的URL地址不完整,没有找到host. 排查解决:发现HTTP请求的URL少加了项目名,导致URL地址不完整.