<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表如下。。没具体涵义。。就是为了实现例子拼凑的。