2011-09-28 22 views
7

當鼠標移動到特定控件上時,是否可以用光標移動Tooltip或類似的東西?工具提示或類似的東西在WPF中移動光標

我試過TextBlock,但Margin屬性不起作用。

private TextBlock tooltip = new TextBlock(); 
    private void imgRoom_MouseEnter(object sender, MouseEventArgs e) 
    {   
     Point position = e.GetPosition((IInputElement)sender); 
     tooltip.Visibility = System.Windows.Visibility.Visible; 
     tooltip.Margin = new Thickness(position.X, position.Y, 0, 0);   
     tooltip.Width = 100; 
     tooltip.Height = 100; 
     tooltip.Background = new SolidColorBrush(Colors.Red); 
    } 

    private void imgRoom_MouseMove(object sender, MouseEventArgs e) 
    { 
     Point position = e.GetPosition((IInputElement)sender); 
     tooltip.Margin = new Thickness(position.X, position.Y, 0, 0); 
    } 
+0

顯示您的代碼,我們可能會看到您的意圖。 –

+0

你的意思是你想讓工具提示隨鼠標光標一起移動嗎?如果是的話,那麼不會失敗工具提示的目的? – loxxy

+0

是的! ir沒有達到工具提示的目的:P 但是我需要一些類似的工具提示去用光標:) – Jalal

回答

10

你可以用Popup和一些簡單的屬性來達到這個效果。從窗口代碼...

<Window x:Class="WpfApplication3.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="MainWindow" Height="350" Width="525"> 
    <Grid> 
    <Rectangle Name="rect" Margin="50,50,0,0" Width="100" Height="100" Fill="LightBlue" MouseMove="Rectangle_MouseMove" MouseLeave="Rectangle_MouseLeave" /> 

    <Popup Name="floatingTip" AllowsTransparency="True" Placement="Relative" PlacementTarget="{Binding ElementName=rect}"> 
     <TextBlock>Look At Me</TextBlock> 
    </Popup> 
    </Grid> 

</Window> 

這就是代碼隱藏的樣子。

... 
private void Rectangle_MouseMove(object sender, MouseEventArgs e) 
{ 
    if (!floatingTip.IsOpen) { floatingTip.IsOpen = true; } 

    Point currentPos = e.GetPosition(rect); 

    // The + 20 part is so your mouse pointer doesn't overlap. 
    floatingTip.HorizontalOffset = currentPos.X + 20; 
    floatingTip.VerticalOffset = currentPos.Y; 
} 

private void Rectangle_MouseLeave(object sender, MouseEventArgs e) 
{ 
    floatingTip.IsOpen = false; 
} 
... 

所以從XAML你可以看到彈出的位置是相對於矩形。當你將鼠標移動到矩形上時,它會變得可見,並且隨着鼠標移動它的位置也會更新。自然,這是一個非常基本的解決方案,但只需稍作調整,處理諸如「MouseEnter」和屬性調整等事件,就可以得到一些非常好的效果。

+0

謝謝!我會盡快實現它:) – Jalal

+2

實際上,您不需要使用Popup創建自己的浮動工具提示,因爲ToolTip無論如何都是Popup。只需將ToolTip的Horizo​​ntalOffset和VerticalOffset設置爲當前的鼠標位置即可。 – henon

2

我不知道這是否是最佳做法,或者如果表現良好,但可以使用Adorner

我已經創建了一個概念證明,但尚未在生產場景(尚)中使用它。裝飾者可以用來創建類似的東西(工具提示),或者自定義鼠標光標或放置目標圖標。

如果裝飾器不需要進行命中測試並且可能直接出現在鼠標位置下,請確保您設置了IsHitTestVisible = false

UPDATE

就完全讀取裝飾器的描述:

的裝飾器常見的應用包括:

  • 添加功能手柄到的UIElement,使用戶能夠操縱元素以某種方式(調整大小,旋轉,重新定位等)。
  • 提供視覺反饋以指示各種狀態或響應各種事件。
  • 在UIElement上覆蓋視覺裝飾。
  • 視覺遮罩或覆蓋部分或全部UIElement。
0

這會使用鼠標光標移動工具提示。

private void OnMouseMoveHandler(object sender, MouseEventArgs args) 
    { 
     if ((sender as FrameworkElement).ToolTip == null) 
      (sender as FrameworkElement).ToolTip = new ToolTip() { Placement = PlacementMode.Relative }; 
     double x = args.GetPosition((sender as FrameworkElement)).X; 
     double y = args.GetPosition((sender as FrameworkElement)).Y; 
     var tip = ((sender as FrameworkElement).ToolTip as ToolTip); 
     tip.Content = tooltip_text; 
     tip.HorizontalOffset = x + 10; 
     tip.VerticalOffset = y + 10; 
    } 
+0

你如何在不同的控件上使用該處理程序?喜歡如果我有兩個不同的按鈕需要這個功能? –

+0

這不適用於在XAML中定義的工具提示文本,即材料另外,如果我以這種方式定義工具提示,我如何確保事件處理程序中的內容是正確的? (tip.Content = ??) –

相關問題