当前位置:编程学习 > C#/ASP.NET >>

wpf中datagrid中如何嵌套?

datagrid中自动生成行时,怎么在某个单元格中嵌套DateTimePicker控件。 --------------------编程问答-------------------- 是自动生成列吧??

某个单元格嵌套DateTimePicker控件??是某列放置的是DateTimePicker控件吧??

首先你要知道如何动态生成列,然后指定动态生成的第几列放置DateTimePicker控件就可以了。


#region 动态生列方法  
        /// <summary>  
        /// 产生模板列(带格式化时间)  
        /// </summary>  
        /// <param name="headername"></param>  
        /// <param name="bindingname"></param>  
        /// <param name="width"></param>  
        /// <returns></returns>  
        public DataGridTemplateColumn CreateDateTimeTemplate(string headername, string bindingname, double width)  
        {  
            DataGridTemplateColumn templateColumn = new DataGridTemplateColumn();  
            templateColumn.Header = headername;  
  
            StringBuilder CellTemp = new StringBuilder();  
  
            CellTemp.Append("<DataTemplate ");  
            CellTemp.Append("xmlns='http://schemas.microsoft.com/winfx/");  
  
            CellTemp.Append("2006/xaml/presentation' ");  
            CellTemp.Append("xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' ");  
  
  
            CellTemp.Append("xmlns:local='clr-namespace:spjl1");  
            CellTemp.Append(";assembly=spjl1'>");  
  
            CellTemp.Append("<Grid>");  
            CellTemp.Append("<Grid.Resources>");  
  
            CellTemp.Append("<local:DateTimeConverter x:Key='DateTimeConverter' />");  
            CellTemp.Append("</Grid.Resources>");  
  
            CellTemp.Append("<TextBlock ");  
            CellTemp.Append("Text = '{Binding " + bindingname + ", ");  
  
            CellTemp.Append("Converter={StaticResource DateTimeConverter}}' ");  
            CellTemp.Append("Margin='4'/>");  
  
            CellTemp.Append("</Grid>");  
            CellTemp.Append("</DataTemplate>");  
  
            templateColumn.CellTemplate = (DataTemplate)XamlReader.Load(CellTemp.ToString());  
            templateColumn.HeaderStyle = (Style)Resources["DataGridHeaderStyle"];  
            templateColumn.CellStyle = (Style)Resources["DataGridCellStyle"];  
            templateColumn.CanUserSort = true;  
            templateColumn.IsReadOnly = true;  
            templateColumn.Width = new DataGridLength(width);  
            return templateColumn;  
        }  
  
        /// <summary>  
        /// 创建DataGridTextColumn模板列  
        /// </summary>  
        /// <param name="columnBindName">需要绑定的字段名</param>  
        /// <param name="columnHeaderName">模板列的Header</param>  
        /// <param name="width">模板列的宽度</param>  
        /// <returns></returns>  
        public DataGridTextColumn CreateDataGridTextColumn(string columnBindName, string columnHeaderName, double width)  
        {  
            DataGridTextColumn dgtextColumn = new DataGridTextColumn();  
            dgtextColumn.Binding = new Binding(columnBindName);  
            dgtextColumn.Header = columnHeaderName;  
            dgtextColumn.HeaderStyle = (Style)Resources["DataGridHeaderStyle"];  
            dgtextColumn.CellStyle = (Style)Resources["DataGridCellStyle"];  
            dgtextColumn.IsReadOnly = true;  
            dgtextColumn.Width = new DataGridLength(width);  
            return dgtextColumn;  
        }  
        #endregion  

http://blog.csdn.net/taomanman/article/details/6741599 ,希望对你有帮助
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,