DataTemple-创建数据模板

来源:互联网 发布:淘宝店铺流失商品 编辑:程序博客网 时间:2024/06/11 00:09
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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 System.Data;
using System.Dynamic;
using System.Windows.Interop;


namespace WpfApplication3
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }


        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            for (int i = 1; i < 10; i++)
            {
                Label labLine = new Label();
                labLine.Content = "message" + i.ToString();


                Button btn = new Button();
                btn.Content = "测试按钮" + i.ToString();


                btn.Click += new RoutedEventHandler(btn_click);


                wpQueyFan.Children.Add(labLine);
                wpQueyFan.Children.Add(btn);




            }


            QueryModel();
            QueryModelNew();


            GetDataGrid();


        }




        private void btn_click(object sender, RoutedEventArgs e)
        {
            Button btn = sender as Button;
            MessageBox.Show(btn.Content.ToString());
        }




        private void QueryModel()
        {


            // Create a DataTable. 
            DataTable table = new DataTable("Product");


            // Create a DataColumn and set various properties. 
            DataColumn column = new DataColumn();
            column.DataType = System.Type.GetType("System.Decimal");
            column.AllowDBNull = false;
            column.Caption = "Price";
            column.ColumnName = "Price";
            column.DefaultValue = 25;


            DataColumn columnName = new DataColumn();
            columnName.DataType = System.Type.GetType("System.String");
            columnName.AllowDBNull = false;
            columnName.Caption = "Name";
            columnName.ColumnName = "Name";
            columnName.DefaultValue = "NA";


            // Add the column to the table. 
            table.Columns.Add(column);
            table.Columns.Add(columnName);


            // Add 10 rows and set values. 
            DataRow row;
            for (int i = 0; i < 1000; i++)
            {
                row = table.NewRow();
                row["Price"] = i + 1;
                row["Name"] = "名称为:" + i.ToString();


                // Be sure to add the new row to the 
                // DataRowCollection. 
                table.Rows.Add(row);
            }


            dgModel.ItemsSource = table.DefaultView;


            dgModel2.ItemsSource = table.DefaultView;


        }


        private void QueryModelNew()
        {


            // Create a DataTable. 
            DataTable table = new DataTable("Product");
            DataRow dr;


            // Create a DataColumn and set various properties. 
            DataColumn column = new DataColumn();
            column.DataType = System.Type.GetType("System.Decimal");
            column.AllowDBNull = false;
            column.Caption = "Price";
            column.ColumnName = "Price";
            column.DefaultValue = 25;


            DataColumn columnName = new DataColumn();
            columnName.DataType = System.Type.GetType("System.String");
            columnName.AllowDBNull = false;
            columnName.Caption = "Name";
            columnName.ColumnName = "Name";
            columnName.DefaultValue = "NA";


            // Add the column to the table. 
            table.Columns.Add(column);
            table.Columns.Add(columnName);


            // Add 10 rows and set values. 
            DataRow row;
            for (int i = 0; i < 1000; i++)
            {
                row = table.NewRow();
                row["Price"] = i + 1;
                row["Name"] = "名称为:" + i.ToString();


                // Be sure to add the new row to the 
                // DataRowCollection. 
                table.Rows.Add(row);
            }




            for (int j = 0; j < table.Rows.Count; j++)
            {


                if (j % 6 == 0)
                {




                }
            }


        }




        private void GetDataGrid()
        {
            DataGridTextColumn dc = new DataGridTextColumn();
            dc.IsReadOnly = true;
            dc.Header = "标题";
            dc.Width = 60;
            dgModel2.Columns.Add(dc);




            DataGridTemplateColumn dgtc = new DataGridTemplateColumn();


            DataTemplate dt = new DataTemplate();
            FrameworkElementFactory fef = new FrameworkElementFactory(typeof (CheckBox));
            Binding binding = new Binding();
            binding.Path = new PropertyPath("MarketIndicator");
            fef.SetBinding(CheckBox.ContentProperty, binding);
            fef.SetValue(CheckBox.ForegroundProperty, Brushes.White);
            dt.VisualTree = fef;
            dgtc.CellTemplate = dt;




            DataGridTemplateColumn dgtc1 = new DataGridTemplateColumn();
            DataTemplate dt1= new DataTemplate();
            FrameworkElementFactory fef1 = new FrameworkElementFactory(typeof(TextBox));
            Binding binding1 = new Binding();
            binding1.Path = new PropertyPath("MarketIndicator");
           // fef1.SetBinding(TextBox.ContentProperty, binding);
            fef1.SetValue(TextBox.ForegroundProperty, Brushes.Blue);
            dt1.VisualTree = fef1;
            dgtc1.CellTemplate = dt1;




            dgModel2.Columns.Add(dgtc1);


            
        }
    }
}
0 0