0

我使用这些代码将数据预览显示为工具提示:

private void dataGrid1_RowLoaded(object sender, RowLoadedEventArgs e)
    {
        var row = e.Row as GridViewRow;
        if (row != null)
        {
            var textBlock = new TextBlock();
            textBlock.Text = "...";
            textBlock.DataContext = row;

            var toolTip = new ToolTip()
            {
                Content = textBlock,
            };

            toolTip.Opened += dataGrid1_ToolTipOpening;
            ToolTipService.SetToolTip(row, toolTip);
        }

    }

    private void dataGrid1_ToolTipOpening(object sender, RoutedEventArgs e)
    {
         GridViewRow row = null;

            var tooltip = e.OriginalSource as ToolTip;
            if (tooltip.Content is UCPreview)
            {
                return;
            }
            else if (tooltip.Content is TextBlock)
            {
                var content = tooltip.Content as TextBlock;
                row = content.DataContext as GridViewRow;
            }
            var gridViewRow = row.Item as DataRowView;
            if (gridViewRow != null)
            {
               //initial UCPreview                    
            }           
    }
}

现在,我希望仅当鼠标位于行的标题上时才显示工具提示。不幸的是 GridViewRow 没有 Header 属性。我该如何解决我的问题?

4

1 回答 1

0

如果我没有误解你的问题,试试这个,你可以通过在 DataGridColumnHeader 样式中设置工具提示来做到这一点,比如

<Window.Resources>
    <Style TargetType="{x:Type DataGridColumnHeader}" x:Key="dghStyle">
        <Setter Property="ToolTip" Value="Red"/>
    </Style>
</Window.Resources>
<Grid>
    <DataGrid ColumnHeaderStyle="{StaticResource dghStyle}">
        <DataGrid.Columns>
            <DataGridCheckBoxColumn/>
        </DataGrid.Columns>
    </DataGrid>
</Grid>

我希望这将有所帮助 。这只是解决方法,您可以根据需要设置 ToolTop。

于 2012-07-15T12:29:53.163 回答