WPF DataGrid的创建,前台代码构建,后台代码构建

1 前台代码

<Window x:Class="DataGridTemplate.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="925" Loaded="Window_Loaded">
    <Window.Resources>

    </Window.Resources>
    <Grid Margin="0,0,-8,0">
        <DataGrid x:Name="myGrid" AutoGenerateColumns="False"   HorizontalAlignment="Left" Margin="0,37,0,0" VerticalAlignment="Top" Width="760" Height="122">
            <DataGrid.Columns >
                <DataGridTextColumn Header="编号" Width=" 80"  Binding="{Binding id,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged }"></DataGridTextColumn>
                <DataGridTextColumn Header=" 姓名" Width=" 80" Binding="{Binding name,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"></DataGridTextColumn>
                <DataGridTextColumn Header=" 年龄" Width=" 80" Binding="{Binding age,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"></DataGridTextColumn>
                <DataGridTemplateColumn Header=" 姓名" Width="80" >
                    <DataGridTemplateColumn.CellTemplate >
                        <DataTemplate>
                            <StackPanel    >
                                <TextBlock  Text="{Binding name}" Background="Yellow"  ></TextBlock>
                                <TextBlock Text="{Binding age}" Background="YellowGreen"      ></TextBlock>
                            </StackPanel>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                    <DataGridTemplateColumn.CellEditingTemplate >
                        <DataTemplate>
                            <ComboBox  x:Name="cboName"  ItemsSource ="{Binding name}">
                            </ComboBox>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellEditingTemplate>

                </DataGridTemplateColumn>

            </DataGrid.Columns>
        </DataGrid>

    </Grid>
</Window>

2 引用到的类

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

namespace DataGridTemplate
{
    class ClsPerson
    {
        public string id
        {
            get;
            set;
        }
        public string name
        {
            get;
            set;
        }
        public int age
        {
            get;
            set;
        }
    }
}

3  后台代码
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 DataGridTemplate
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            List<ClsPerson >  list=new List<ClsPerson> ();
            ClsPerson  p1=new ClsPerson ();
            ClsPerson  p2=new ClsPerson ();
            p1.id = "1";
            p1.name = "harden";
            p1.age = 18;
            p2.id = "2";
            p2.name = "jimmy";
            p2.age = 20;
            list.Add(p1);
            list.Add(p2);
            myGrid.ItemsSource = list;
            //*********************************************************
            DataGridTextColumn objColumn = new DataGridTextColumn();
            objColumn.Header = "后台创建年龄列";
            objColumn.Width = 80;
            objColumn.Binding = new Binding("age");
            myGrid.Columns.Add(objColumn);
            //*******************************************************************************
            DataGridTemplateColumn objTemColumn = new DataGridTemplateColumn();
            DataTemplate objTem = new DataTemplate();
            StackPanel objStackPanel = new StackPanel();
            TextBlock objTextBlock = new TextBlock();

            FrameworkElementFactory spFactory = new FrameworkElementFactory(typeof(StackPanel));
            spFactory.Name = "myComboFactory";
            spFactory.SetValue(StackPanel.OrientationProperty, Orientation .Vertical );
            
           
            FrameworkElementFactory myTextBlock = new FrameworkElementFactory(typeof(TextBlock));
            myTextBlock.SetBinding(TextBlock.TextProperty, new Binding("age"));
            spFactory.AppendChild(myTextBlock);

            FrameworkElementFactory myTextBlock2 = new FrameworkElementFactory(typeof(TextBlock));
            myTextBlock2.SetBinding(TextBlock.TextProperty, new Binding("name"));
            spFactory.AppendChild(myTextBlock2);
            objTem.VisualTree = spFactory;

            objTemColumn.CellTemplate = objTem;
            objTemColumn.Header = "后台创建姓名模板列";
            objTemColumn.Width = 80;
            myGrid.Columns.Add(objTemColumn);
        }

    }
}


阅读更多
个人分类: .NET
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭