2016-08-03 34 views
1

Ich möchte außerhalb der Band für TextBox verwenden RibbonToolTip.Wie stellt RibbonToolTip als globale Tooltip

Der lange Weg ist:

<TextBox Text="{Binding Name}"> 
    <TextBox.ToolTip> 
    <RibbonToolTip Title="{DynamicResource nameTitle}" Description="{DynamicResource nameDescription}"/> 
    </TextBox.ToolTip> 
</TextBox> 

ich so etwas wie dies stattdessen

<TextBox Text="{Binding Name}" ToolTipTitle="{DynamicResource nameTitle}" ToolTipDescription="{DynamicResource nameDescription}"/> 

ich die RibbonToolTip einstellen verwenden möchten mit

<Style TargetType="TextBox"> 
    <Setter Property="ToolTip" > 
    <Setter.Value> 
     <RibbonToolTip/> 
    </Setter.Value> 
    </Setter> 
</Style> 

aber wie Titel zu setzen und Beschreibung.

Gibt es eine Möglichkeit, eine neue Klasse von TextBox ohne Ableitung neue Abhängigkeitsobjekte für ToolTipTitle und ToolTipDescription zu schaffen?

<TextBox ToolTip.Title="Name" ... 

funktioniert auch nicht, weil ToolTip nicht eingegeben wird.

Antwort

0

Die Antwort ist angebracht Eigenschaften.

public class ExToolTip : DependencyObject 
    { 
     public static readonly DependencyProperty TitleProperty = DependencyProperty.RegisterAttached("Title", typeof(string), typeof(ExToolTip), new PropertyMetadata("", OnTitleChanged)); 

     public static string GetTitle(DependencyObject o) 
     { 
      return (string)o.GetValue(TitleProperty); 
     } 

     public static void SetTitle(DependencyObject o, string value) 
     { 
      o.SetValue(TitleProperty, value); 
     } 

     private static void OnTitleChanged(DependencyObject o, DependencyPropertyChangedEventArgs e) 
     { 
      FrameworkElement fe = o as FrameworkElement; 
      if (fe != null) 
      { 
       if (fe.ToolTip == null) 
       { 
        fe.ToolTip = new RibbonToolTip(); 

       } 
       RibbonToolTip rtt = fe.ToolTip as RibbonToolTip; 
       if (rtt != null) 
       { 
        rtt.Title = (string)e.NewValue; 
       } 
      } 
     } 

     public static readonly DependencyProperty DescriptionProperty = DependencyProperty.RegisterAttached("Description", typeof(string), typeof(ExToolTip), new PropertyMetadata("", OnDescriptionChanged)); 

     public static string GetDescription(DependencyObject o) 
     { 
      return (string)o.GetValue(DescriptionProperty); 
     } 

     public static void SetDescription(DependencyObject o, string value) 
     { 
      o.SetValue(DescriptionProperty, value); 
     } 

     private static void OnDescriptionChanged(DependencyObject o, DependencyPropertyChangedEventArgs e) 
     { 
      FrameworkElement fe = o as FrameworkElement; 
      if (fe != null) 
      { 
       if (fe.ToolTip == null) 
       { 
        fe.ToolTip = new RibbonToolTip(); 

       } 
       RibbonToolTip rtt = fe.ToolTip as RibbonToolTip; 
       if (rtt != null) 
       { 
        rtt.Description = (string)e.NewValue; 
       } 
      } 
     } 
    } 

Mit dieser Klasse kann ich einfach schreiben:

<TextBox Text="{Binding Text}" ExToolTip.Title="title" ExToolTip.Description="Description" />