代码创建Silverlight 中DataGrid的模板列

来源:转载

设计时在DataGrid中设置模板列很容易,也很方便。但是在运行期,能过代码动态创建 DataGrid列及模板列,却不是那么的方便了。

好,其它也就不说了,放上代码:

private DataGridTextColumn CreateTextColumn(string fieldname)        {
            DataGridTextColumn column = new DataGridTextColumn();
            column.Binding = new Binding(fieldname);
            return column;
        }
        private DataGridTextColumn CreateDateColumn(string fieldname)
        {
            DataGridTextColumn column = new DataGridTextColumn();
            Binding binding = new Binding(fieldname);
            binding.Converter = new DateConverter();
            column.Binding = binding;
            return column;
        }
        private DataGridTemplateColumn CreateHyperlinkColumn(string fieldname, string navigateUrl)
        {
            const string templateContent = @"<DataTemplate xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation"">
<HyperlinkButton Content=""{{Binding {0}}}"" NavigateUri=""{1}"" TargetName=""_blank"" VerticalAlignment=""Center"" />
</DataTemplate>";
            string templateStr = string.Format(templateContent, fieldname, navigateUrl);
            DataTemplate template = (DataTemplate)XamlReader.Load(templateStr);
            DataGridTemplateColumn column = new DataGridTemplateColumn();
            column.CellTemplate = template;
            return column;
        }
        private DataGridCheckBoxColumn CreateCheckBoxColumn(string fieldname)
        {
            DataGridCheckBoxColumn column = new DataGridCheckBoxColumn();
            column.Binding = new Binding(fieldname);
            return column;
       }

本文来自bloglife的博客,原文地址:http://blog.csdn.net/bloglife/archive/2010/03/25/5414652.aspx


分享给朋友:
您可能感兴趣的文章:
随机阅读: