2016-04-22 4 views
0

XAMLでの行動でのTabItemを追加します。WPFは、プログラム

<!-- XAML --> 
<TabItem behaviors:TabItemValidationBehavior.ActivateValidation ="True"> 
<TabItem.Header> 
    <TextBlock Text="Header"     
       Foreground="{Binding Path=(behavior:TabItemBehavior.Foreground), RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type TabItem}}}" /> 
    </TabItem.Header> 
</TabItem> 

をプログラムで同じことを行うことは可能でしょうか?

// C# 
TabItem tab = new TabItem(); 
??tab.AddBehavior(behaviors:TabItemValidationBehavior.ActivateValidation(True));?? 
??tab.Header= new TextBlock { Foreground.BindTo(behavior:TabItemBehavior.Foreground, tab) };?? 

これを達成する方法は?

答えて

1

動作はAttachedPropertyを公開します。あなたはそれを好きなように設定できます

TabItem tab = new TabItem(); 
TabItemValidationBehavior.SetActivateValidation(tab, true); 

TextBlock text = new TextBlock(); 
Binding binding = new Binding(); 
binding.Path = new PropertyPath(TabItemBehavior.ForegroundProperty); 
binding.RelativeSource = new RelativeSource{Mode = RelativeSourceMode.FindAncestor, AncestorType = typeof(TabItem)}; 

text.SetBinding(TextBlock.ForegroundProperty, binding); 

tab.Header=text; 
+0

ありがとう、ありがとうございます。私は解決策から遠く離れていた;) –

関連する問題