2011-04-29 43 views
0

Silverlight的4 DescriptionViewer控制顯示工具提示的DescriptionSilverlight的DescriptionViewer防止工具提示上點擊消失

的語法很簡單:添加命名空間xmlns:dataInput="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data.Input"和下面的XAML到您的用戶控件:

<dataInput:DescriptionViewer Description="Some hints on user input etc." /> 

不幸的是,一些用戶點擊顯示的圖標(因爲它甚至默認懸停),它立即關閉工具提示(實際和唯一的信息)。更糟糕的是,當您在懸停後快速點擊時,Tooltip甚至不會出現,因此用戶可能會認爲Icon完全沒有功能。

我想阻止關閉點擊工具提示(只需關閉「鼠標移出」),甚至更好的點擊應該強制工具提示立即顯示(在顯示之前跳過通常的超時)。

雖然我似乎很難,因爲沒有OnClick事件,而且MouseLeftButtonDown似乎根本不會觸發。我也嘗試覆蓋DescriptionViewer控制,但也沒有找到合適的方法來覆蓋。

你能幫忙嗎?謝謝!

回答

1

這實際上與DescriptionViewer無關,它是ToolTip的行爲。一旦你點擊鼠標,工具提示就會消失。在這種情況下,你可能想寫你自己的工具提示。

我覺得通常當你點擊DescriptionViewer圖標時,應該會打開一個新窗口,就像更詳細的幫助頁面一樣。所以用戶不會感到困惑。

更新:

您可以通過定義附加屬性實現這一目標。基本上,您將此屬性附加到DescriptionViewer中的按鈕。當按鈕的Click事件觸發時,您可以在Button下找到Tooltip,並將其IsOpen設置爲ture。然後,您還需要處理MouseLeave事件,以便在鼠標移開後隱藏Tooltip。

這是如何定義附加屬性。

公共靜態類ButtonAttachedProperties { 公共靜態布爾GetOpenToolTip(DependencyObject的OBJ) { 回報(布爾)obj.GetValue(OpenToolTipProperty); }

public static void SetOpenToolTip(DependencyObject obj, bool value) 
{ 
    obj.SetValue(OpenToolTipProperty, value); 
} 

public static readonly DependencyProperty OpenToolTipProperty = 
    DependencyProperty.RegisterAttached("OpenToolTip", typeof(bool), typeof(ButtonAttachedProperties), new PropertyMetadata(false, Callback)); 


private static void Callback(DependencyObject d, DependencyPropertyChangedEventArgs e) 
{ 
    var button = d as Button; 

    if (button == null || !(bool)e.NewValue) return; 

    button.Click += (s, e1) => 
    { 
     var tooltip = button.FindName("MyToolTip") as ToolTip; 

     if (tooltip != null) 
     { 
      tooltip.PlacementTarget = button; 
      tooltip.IsOpen = true;  
     } 
    }; 

    button.MouseLeave += (s, e2) => 
    { 
     var tooltip = button.FindName("MyToolTip") as ToolTip; 

     if (tooltip != null) 
      tooltip.IsOpen = false; 
    }; 
} 

}在DescriptionViewer的風格

然後,您將附加屬性按鈕。您還需要命名工具提示,以便能夠使用附加屬性類中的查找名稱來查找它。

     <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Height="{TemplateBinding Height}" Padding="{TemplateBinding Padding}" Width="{TemplateBinding Width}"> 
          <Button x:Name="DescriptionContent" local:ButtonAttachedProperties.OpenToolTip="True" BorderBrush="#FFFFFFFF" BorderThickness="1" Background="#00000000" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" IsTabStop="False" Padding="1" Template="{TemplateBinding GlyphTemplate}" Visibility="Collapsed" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"> 
           <ToolTipService.ToolTip> 
            <ToolTip x:Name="MyToolTip" Content="{TemplateBinding Description}" PlacementTarget="{Binding RelativeSource={RelativeSource TemplatedParent}}" Style="{TemplateBinding ToolTipStyle}"/> 
           </ToolTipService.ToolTip> 
          </Button> 
         </Border> 

希望這會有所幫助。 :)

+0

「應該有一個新的窗口打開,就像一個更詳細的幫助頁面」 - 是爲什麼不,但我仍然無法處理鼠標點擊做到這一點。看起來我應該寫我自己的DescriptionViewer。 – thmshd 2011-04-30 20:38:26

+0

如果你只是想點擊鼠標,那很容易。您可以編輯descrption查看器的默認樣式,並在模板內部找到名爲DescriptionContent的按鈕,並從那裏處理其Click事件。 – 2011-04-30 23:28:42

+0

好吧,我明白這背後的概念,但我實際上並沒有覺得它很容易做到這一點在一個優雅的,可重用的方式:) – thmshd 2011-05-02 21:26:31