2016-09-26 101 views
0

如果我只創建一個工具提示並將它的staysopen屬性設置爲false,它的工作。但是,如果我創建多個工具提示並將其staysopen屬性設置爲false,則只有在鼠標單擊後,我創建的第一個工具提示纔會關閉。其他人保持開放。Wpf多個工具提示staysopen屬性不起作用

爲什麼?

示例代碼:

ToolTip tooltip = new ToolTip { Content = "Password cannot be empty." }; 

Password_PasswordBox.ToolTip = tooltip; 
tooltip.PlacementTarget = Password_PasswordBox; 
tooltip.Placement = System.Windows.Controls.Primitives.PlacementMode.Top; 
tooltip.IsOpen = true; 
tooltip.StaysOpen = false; 

ToolTip tooltip2 = new ToolTip { Content = "Username cannot be empty." }; 

Username_TextBox.ToolTip = tooltip2;    
tooltip2.PlacementTarget = Username_TextBox; 
tooltip2.Placement = System.Windows.Controls.Primitives.PlacementMode.Top; 
tooltip2.IsOpen = true; 
tooltip2.StaysOpen = false; 

回答

0

您正在迫使這兩個工具提示打開,然後而不是讓他們繼續開放,這是造成異常。

System.NotSupportedException了未處理 消息:「System.NotSupportedException」類型的未處理的異常出現在PresentationFramework.dll 附加信息:默認情況下,工具提示屬性不支持工具提示元素與StaysOpen設置爲false。

如果你希望他們繼續開放更新ToolTipService.ShowDurationProperty

 ToolTipService.ShowDurationProperty.OverrideMetadata(typeof(DependencyObject), new FrameworkPropertyMetadata(Int32.MaxValue)); 

     { 
      ToolTip tooltip = new ToolTip {Content = "Password cannot be empty."}; 

      Password_PasswordBox.ToolTip = tooltip; 
      tooltip.PlacementTarget = Password_PasswordBox; 
      tooltip.Placement = System.Windows.Controls.Primitives.PlacementMode.Top; 
      tooltip.IsOpen = true; 
      //tooltip.StaysOpen = false; 
     } 
     { 
      ToolTip tooltip2 = new ToolTip { Content = "Username cannot be empty." }; 

      Username_TextBox.ToolTip = tooltip2; 
      tooltip2.PlacementTarget = Username_TextBox; 
      tooltip2.Placement = System.Windows.Controls.Primitives.PlacementMode.Top; 
      tooltip2.IsOpen = true; 
      //tooltip2.StaysOpen = false; 
     }