2016-04-22 5 views
0

in XAML, wenn ich ein bestimmtes Verhalten hinzufügen möchte ich wie folgt tun:WPF hinzufügen TabItem mit Verhalten programmatisch

<!-- 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> 

Es ist möglich, die gleiche programmatisch zu tun?

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

Wie erreicht man das?

Antwort

1

Verhalten zeigt eine AttachedProperty an. Sie können es wie

setzen
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

Danke, funktioniert gut. Ich war weit weg von der Lösung;) –

Verwandte Themen