如何使用转换器动态获取工具提示?

时间:2016-02-23 11:32:39

标签: wpf vb.net xaml

如果我运行我的项目,我的工具提示转换器运行一次 - 我需要它每次鼠标悬停在一行上时运行。

这是我的XAML:

background-color: beige !important; 

代码......

<Window x:Class="MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApplication1"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <local:MyTooltipConverter x:Key="MyTooltipConverter" />
    </Window.Resources>
    <Grid>
        <DataGrid x:Name="dataGrid" ItemsSource="{Binding}" HorizontalAlignment="Left" VerticalAlignment="Top" Height="263" Width="507">
            <DataGrid.Resources>
                <Style TargetType="{x:Type DataGridRow}">
                    <Setter Property="ToolTip">
                        <Setter.Value>
                            <ToolTip Content="{Binding ??, Converter={StaticResource MyTooltipConverter}}" />
                        </Setter.Value>
                    </Setter>
                </Style>
            </DataGrid.Resources>
        </DataGrid>
    </Grid>
</Window>

如何通过当前时间调用MyTooltipConveter获取工具提示?

由于

1 个答案:

答案 0 :(得分:1)

将MyTooltipConverter.Convert方法更改为Return面板。返回tip会抛出System.InvalidOperationException - 'ToolTip'不能有逻辑或可视父级。

至于绑定,只需使用转换器即可。

<ToolTip Content="{Binding Converter={StaticResource MyTooltipConverter}}" />

当然,在更新时,您必须移动鼠标以生成新的工具提示。如果你想要的是一个不断更新的工具提示,那么你需要添加一个计时器并更新block.Text。

这样的事情:

Public Class MyTooltipConverter
    Implements IValueConverter

    Public Function Convert(value As Object, targetType As Type, parameter As Object, culture As CultureInfo) As Object Implements IValueConverter.Convert
        If value Is Nothing Then
            Return Nothing
        End If

        Dim panel = New StackPanel()
        panel.Orientation = Orientation.Vertical

        Dim block As New TextBlock()
        block.Text = Now.ToString
        panel.Children.Add(block)

        Dim timer As New System.Windows.Threading.DispatcherTimer()
        timer.Interval = New TimeSpan(0, 0, 1)
        AddHandler timer.Tick, Sub()
                                   block.Text = Now.ToString
                               End Sub
        timer.Start()
        Debug.WriteLine("Timer Started")

        AddHandler panel.Unloaded, Sub(s, e)
                                       timer.Stop()
                                       timer = Nothing
                                       panel = Nothing
                                       Debug.WriteLine("Timer Stopped")
                                   End Sub

        Return panel
    End Function

    Public Function ConvertBack(value As Object, targetType As Type, parameter As Object, culture As CultureInfo) As Object Implements IValueConverter.ConvertBack
        Throw New NotImplementedException()
    End Function
End Class