• WPF DataGrid 的RowDetailsTemplate的使用


    <Window x:Class="DataGridExam.DataGridRowDetailsExam"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="DataGridRowDetailsExam" Height="300" Width="300">
        <Grid>
            <DataGrid Name="gridProducts" Margin="5" AutoGenerateColumns="False" RowDetailsVisibilityMode="VisibleWhenSelected">
                <DataGrid.RowDetailsTemplate>
                    <DataTemplate>
                        <Border Margin="10" Padding="10" BorderBrush="SteelBlue" BorderThickness="3"
                                CornerRadius="5">
                            <TextBlock Text="{Binding Path=Description}" TextWrapping="Wrap"
                                       FontSize="13" MaxWidth="300" TextAlignment="Left"></TextBlock>
                        </Border>
                    </DataTemplate>
                </DataGrid.RowDetailsTemplate>

                <DataGrid.Columns>
                    <DataGridTextColumn Header="Product" Width="175" Binding="{Binding Path=ModelName}"></DataGridTextColumn>
                    <DataGridTextColumn Header="Price" Binding="{Binding Path=UnitCost,StringFormat={}{0:C}}"></DataGridTextColumn>
                    <DataGridTextColumn Header="ModelNumber" Binding="{Binding Path=ModelNumber}"></DataGridTextColumn>
                </DataGrid.Columns>
            </DataGrid>
        </Grid>

    </Window>

    using ClassLibrary;
    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;


    namespace DataGridExam
    {
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
                gridProducts.ItemsSource = StoreDB.GetProducts();
            }
           


            private void gridProducts_LoadingRow_1(object sender, DataGridRowEventArgs e)
            {
                Product p = (Product)e.Row.DataContext;
                if (p.UnitCost >= 10)
                {
                    e.Row.Background = new SolidColorBrush(Colors.Orange);
                }
                else
                {
                    e.Row.Background = new SolidColorBrush(Colors.White);
                }
            }
        }

    }


    using System;
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.Data;
    using System.Data.SqlClient;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;


    namespace ClassLibrary
    {
        public class StoreDB
        {
            public static string connString = Properties.Settings.Default.ConnectionString;




            public static Product GetProductByID(int id)
            {
                Product p = null;
                SqlConnection con = new SqlConnection(connString);
                SqlCommand cmd = new SqlCommand("GetProductByID", con);
                cmd.CommandType = System.Data.CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@ProductID", id);
                try
                {
                    con.Open();
                    SqlDataReader reader = cmd.ExecuteReader();
                    while (reader.Read())
                    {
                        p = new Product()
                        {
                            CategoryID = (int)reader[1],
                            ModelNumber = reader[2].ToString(),
                            ModelName = reader[3].ToString(),
                            ProductImage=reader[4].ToString(),
                            UnitCost = (decimal)reader[5],
                            Description = reader[6].ToString()
                        };
                    }
                    return p;


                }
                catch (Exception)
                {


                    throw;
                }
                finally
                {
                    con.Close();
                }
            }


            public static void UpdateProductByID(int ProductID,Product p)
            {
                SqlConnection con = new SqlConnection(connString);
                SqlCommand cmd = new SqlCommand("UpdateProductByID", con);
                cmd.CommandType = System.Data.CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@ProductID",ProductID);
                cmd.Parameters.AddWithValue("@CategoryID",p.CategoryID);
                cmd.Parameters.AddWithValue("@ModelNumber",p.ModelNumber);
                cmd.Parameters.AddWithValue("@ModelName",p.ModelName);
                cmd.Parameters.AddWithValue("@ProductImage",p.ProductImage);
                cmd.Parameters.AddWithValue("@UnitCost",p.UnitCost);
                cmd.Parameters.AddWithValue("@Description",p.Description);
                try
                {
                    con.Open();
                    cmd.ExecuteNonQuery();
                }
                catch (Exception)
                {


                    throw;
                }
                finally
                {
                    con.Close();
                }
            }


            public static void InsertProduct(Product p)
            {
                SqlConnection con = new SqlConnection(connString);
                SqlCommand cmd = new SqlCommand("InsertProduct", con);
                cmd.CommandType = System.Data.CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@CategoryID", p.CategoryID);
                cmd.Parameters.AddWithValue("@ModelNumber", p.ModelNumber);
                cmd.Parameters.AddWithValue("@ModelName", p.ModelName);
                cmd.Parameters.AddWithValue("@ProductImage", p.ProductImage);
                cmd.Parameters.AddWithValue("@UnitCost", p.UnitCost);
                cmd.Parameters.AddWithValue("@Description", p.Description);
                try
                {
                    con.Open();
                    cmd.ExecuteNonQuery();
                }
                catch (Exception)
                {


                    throw;
                }
                finally
                {
                    con.Close();
                }
            }


            public static void DeleteProductByID(int id)
            {
                SqlConnection con = new SqlConnection(connString);
                SqlCommand cmd = new SqlCommand("DeleteProductByID", con);
                cmd.CommandType = System.Data.CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@ProductID", id);
                try
                {
                    con.Open();
                    cmd.ExecuteNonQuery();
                }
                catch (Exception)
                {


                    throw;
                }
                finally
                {
                    con.Close();
                }
            }




            public static ObservableCollection<Product> GetProducts()
            {
                ObservableCollection<Product> products = new ObservableCollection<Product>();
                SqlConnection con = new SqlConnection(connString);
                SqlCommand cmd = new SqlCommand("GetProducts", con);
                cmd.CommandType = System.Data.CommandType.StoredProcedure;
                try
                {
                    con.Open();
                    SqlDataReader reader = cmd.ExecuteReader();
                    while (reader.Read())
                    {
                        products.Add(new Product()
                        {
                            ProductID = (int)reader[0],
                            CategoryID = (int)reader[1],
                            ModelNumber = reader[2].ToString(),
                            ModelName = reader[3].ToString(),
                            ProductImage = reader[4].ToString(),
                            UnitCost = (decimal)reader[5],
                            Description = reader[6].ToString()
                        });
                    }
                    return products;


                }
                catch (Exception)
                {
                    throw;
                }
                finally
                {
                    con.Close();
                }
            }




            public static DataSet GetProductsAndCategories()
            {
                SqlConnection con = new SqlConnection(connString);
                SqlCommand cmd = new SqlCommand("GetProducts", con);
                cmd.CommandType = System.Data.CommandType.StoredProcedure;
                DataSet ds = new DataSet();
                SqlDataAdapter adatper = new SqlDataAdapter(cmd);
                adatper.Fill(ds, "Products");
                cmd.CommandText = "GetCategories";
                adatper.Fill(ds, "Categories");
                DataRelation rel = new DataRelation("CategoryProduct", ds.Tables["Categories"].Columns["CategoryID"], ds.Tables["Products"].Columns["CategoryID"]);
                return ds;
            }
        }

    }


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;


    namespace ClassLibrary
    {
        public class Product
        {
            public int ProductID { get; set; }
            public int CategoryID { get; set; }
            public string ModelNumber { get; set; }
            public string ModelName { get; set; }
            public string ProductImage { get; set; }
            public decimal UnitCost { get; set; }
            public string Description { get; set; }


            public Product(int CategoryID = 0, string ModelNumber = "",
                string ModelName = "", string ProductImage = "", decimal UnitCost=0,string Description="")
            {
                this.CategoryID = CategoryID;
                this.ModelNumber = ModelNumber;
                this.ModelName = ModelName;
                this.ProductImage = ProductImage;
                this.UnitCost = UnitCost;
                this.Description = Description;
            }


        }
    }

  • 相关阅读:
    Google快照查看八大绝招[小摘]
    CSS li或dd 浮动后增加图片时多出4PX问题
    ASP.NET C# 邮件发送全解
    ff ie7 ie6 css支持问题[拼网页标记]
    net 中 等同与ajax的效果 ICallbackEventHandler
    让DIV层位于flash对象之上
    屏蔽JS错误代码
    META标签的使用摘录,网页跳转特效
    【转】海量数据处理常用思路和方法
    MFC画图定时刷新,能否做到完全无闪烁
  • 原文地址:https://www.cnblogs.com/dxmfans/p/9434797.html
Copyright © 2020-2023  润新知