2012-02-15 15 views
1

私は素敵なFrameworkelementfactoryを使用してContentTemplateを作成しようとしています。ボタンの内容がFrameworkelementfactoryを使用して更新されていません

ボタンのコンテンツを設定することはできませんが、コードは機能しません。私は多くのことを試しましたが、私はいつもContent = Buttonのボタンで終わります。

ここにcontenttemplateを生成するコードを示します。あなたの詳細については、私はTabcontrolヘッダーItemTemplateでこれを使用しています...

乾杯。

ControlTemplate ct = new ControlTemplate(typeof(TabItem)); 

FrameworkElementFactory spouter = new FrameworkElementFactory(typeof (DockPanel)); 
FrameworkElementFactory text = new FrameworkElementFactory(typeof(TextBlock)); 
text.SetValue(TextBlock.TextProperty, Name); 
spouter.AppendChild(text); 

FrameworkElementFactory mButtonPrev = new FrameworkElementFactory(typeof(Button)); 
mButtonPrev.SetValue(System.Windows.Controls.Button.ContentProperty, "x"); 
mButtonPrev.AddHandler(System.Windows.Controls.Button.ClickEvent, new RoutedEventHandler(CloseTab)); 
spouter.AppendChild(mButtonPrev); 
ct.VisualTree = spouter; 
return ct; 

答えて

0
ControlTemplate ct = new ControlTemplate(typeof(TabItem)); 

あなたの代わりに、ここでDataTemplateを作成すべきではありませんか?

(他のすべては、私には正常に見える、またFEFは廃止され、the docsを読む)

+0

こんにちは。そのTabItemと私はTabItem.Templateを設定していますが、XAMLはDatatemplate型ですがDatatemplateにキャストできません。 Controltemplateの作品を使用して! Xaml.loadを使用するのは本当に遅く、キャストできません。そして、はい、私はそれが非難されて読んでいる。しかし、ハンドラはなぜ機能しますが、コンテンツは動作しませんか? – stackeroverflow

0

まだFEFを使用して、使用する人のために、私は実際の文字列と私のボタンの内容を設定することができました。私はあなたの例で "ボタン"がどこから来ているかという名前を参照しています。私の例では、Nameは私のDataGridにバインドしているクラス名をプルアップします。

var buttonTemplate = new FrameworkElementFactory(typeof(Button)); 
    var text = new FrameworkElementFactory(typeof(TextBlock)); 
    text.SetValue(TextBlock.TextProperty, "Save"); 
    buttonTemplate.AppendChild(text); 
    buttonTemplate.AddHandler(
     System.Windows.Controls.Primitives.ButtonBase.ClickEvent, 
     new RoutedEventHandler((o, e) => MessageBox.Show("hi")) 
    ); 

    AccountDatagrid.Columns.Add(new DataGridTemplateColumn() 
    { 
     Header = "Save", 
     CellTemplate = new DataTemplate() { VisualTree = buttonTemplate } 
    }); 
    AccountDatagrid.ItemsSource = AccoutDescCodeTime.GetBaseAccounts(); 
関連する問題