如何从XAML为Style中的控件指定工具提示?

Sea*_*ner 14 wpf datagrid tooltip

我正在使用Microsoft CodePlex项目中的WPF数据网格.我有一个自定义控件,我想从数据网格的行数据绑定到一个字段.我不能为我的生活弄清楚如何在datagrid行上指定工具提示.

我最接近的是使用带有Setter的RowStyle来设置工具提示,但这似乎只适用于文本.当我尝试将ControlTempalte作为ToolTip的值时,它会显示在ControlTemplate类型上调用ToString的结果.

我想我需要设置ToolTip的"Template"属性,但我似乎无法弄清楚如何做到这一点......

  <dg:DataGrid Name="dgResults" AutoGenerateColumns="True">

            <dg:DataGrid.RowStyle >


            <Style TargetType="{x:Type dg:DataGridRow}">

                <Setter Property="ToolTip"  >
                    <Setter.Value>

                        <ControlTemplate TargetType="{x:Type ToolTip}">
                           <StackPanel>
                                 <TextBlock>txt1</TextBlock><TextBlock>txt2</TextBlock>
                           </StackPanel>
                        </ControlTemplate>


                    </Setter.Value>
                </Setter>
            </Style>

        </dg:DataGrid.RowStyle>

  </dg:DataGrid>
Run Code Online (Sandbox Code Playgroud)

Sea*_*ner 26

想出来......花了我大约6个小时......

出于某种原因,我无法使用Value.Setter直接设置值.如果我将工具提示的内容定义为静态资源,然后在DataGrid.RowStyle的Style属性中设置它,它就可以工作.

因此,datagrid行样式如下所示:

            <Style TargetType="{x:Type dg:DataGridRow}">

                <Setter Property="ToolTip" Value="{StaticResource resKWIC}">
                </Setter>                 
            </Style>

        </dg:DataGrid.RowStyle>
Run Code Online (Sandbox Code Playgroud)

资源是

<Window.Resources>
    <StackPanel x:Key="resKWIC">
        <TextBlock>f1</TextBlock>
        <TextBlock>f2></TextBlock>
    </StackPanel>
</Window.Resources>
Run Code Online (Sandbox Code Playgroud)

谢谢!


小智 6

关键是使用Property ToolTipService.ToolTip而不是ToolTip - 如下所示:

<Setter Property="ToolTipService.ToolTip" Value="My Tooltip"/>
Run Code Online (Sandbox Code Playgroud)