【问题标题】:Assign a control template programmatically with XamDataGrid使用 XamDataGrid 以编程方式分配控件模板
【发布时间】:2013-11-06 19:24:11
【问题描述】:

大家下午好,

我们的 WPF 应用程序 (VS 2010) 上有一个 XamDataGrid,我需要以编程方式为一个字段添加一个控件模板(字段总是从我们应用程序的代码中添加)。

因此,我不是在 XAML 上创建模板,而是从代码创建它(我关注了几篇解释类似问题的帖子),如下所示:

        Field f = new Field { Name = name, Label = label, Width = new FieldLength(20) }; 
        Style cellStyle = new System.Windows.Style(); 

        string templateStr = "<ControlTemplate TargetType=\"{x:Type igDP:CellValuePresenter}\">" + 
                                 "<Button Content=\"Flu\" />" + 
                            "</ControlTemplate>"; 

        ParserContext pc = new ParserContext(); 
        pc.XmlnsDictionary.Add("", "http://schemas.microsoft.com/winfx/2006/xaml/presentation"); 
        pc.XmlnsDictionary.Add("x", "http://schemas.microsoft.com/winfx/2006/xaml"); 
        pc.XmlnsDictionary.Add("igDP", "http://infragistics.com/DataPresenter"); 

        ControlTemplate ct = (ControlTemplate)XamlReader.Parse(templateStr, pc); 

        cellStyle.Setters.Add(new Setter() { Property = CellValuePresenter.TemplateProperty, Value = ((object)new CellValuePresenter() { Template = ct }) }); 
        f.Settings.CellValuePresenterStyle = cellStyle; 


        MainDataGrid.FieldLayouts[0].Fields.Add(f); 

不知何故,这不起作用,我的任何字段/列都没有显示。所以这不起作用,任何帮助将不胜感激。

【问题讨论】:

  • 你的方法对我来说似乎很奇怪。在真正的 XAML 中执行此操作,而不是这种怪物。

标签: c# wpf infragistics xamdatagrid


【解决方案1】:

好的!我找到了!

问题在于我将 setter 添加到 ControlTemplate 的方式,所以不是:

cellStyle.Setters.Add(new Setter() { Property = CellValuePresenter.TemplateProperty, Value = ((object)new CellValuePresenter() { Template = ct }) });

应该是这样的:

cellStyle.Setters.Add(new Setter(CellValuePresenter.TemplateProperty, ct));

然后它就起作用了!

我知道这不是最好的方法,但我被要求这样做。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-12-09
    • 2011-10-19
    • 1970-01-01
    • 2011-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多