0

皆さん、こんにちは。

WPF アプリケーション (VS 2010) に XamDataGrid があり、1 つのフィールドのコントロール テンプレートをプログラムで追加する必要があります (フィールドは常にアプリのコードから追加されます)。

したがって、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); 

どういうわけかこれが機能せず、フィールド/列が表示されません。したがって、これは機能していません。助けていただければ幸いです。

4

1 に答える 1

0

Ok!見つけた!

問題は、セッターを ControlTemplate に追加する方法にあったため、代わりに:

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

次のようになります。

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

そして、それは機能します!

それが最善の方法ではないことはわかっていますが、それが私に求められた方法です。

于 2013-11-07T14:06:08.273 に答える