如何在XAML中为样式中的控件指定ToolTip?

14
我正在使用来自Microsoft CodePlex项目的WPF数据网格。我有一个自定义控件,我想将其数据绑定到数据网格行的字段。我无法弄清楚如何在数据网格行上指定工具提示。
我最接近的方法是使用RowStyle和Setter设置工具提示,但这似乎仅适用于文本。当我尝试将ControlTemplate作为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>

@Sean - 至少可以说是很烦人的。花了15-20分钟来解决这个问题...我很想知道为什么会这样... - Gishu
6个回答

28

我搞定了……花了我约6个小时。

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

因此,数据网格行样式如下:

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

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

        </dg:DataGrid.RowStyle>

而且这个资源是

<Window.Resources>
    <StackPanel x:Key="resKWIC">
        <TextBlock>f1</TextBlock>
        <TextBlock>f2></TextBlock>
    </StackPanel>
</Window.Resources>

谢谢!


7
关键是要使用 Property ToolTipService.ToolTip,而不是 ToolTip - 就像这样:
<Setter Property="ToolTipService.ToolTip" Value="My Tooltip"/>

1

我也通过一些更改让这个工作起来,分享出来以便有帮助的人使用。

我的Datadrid绑定到一个自定义对象列表,我想要显示字符串"Name"作为列,将字符串"text"显示在工具提示中。对于我这个新手而言,诀窍是我必须包含文本列并将其隐藏才能在工具提示中显示,即:

<DataGrid AutoGenerateColumns="False" CanUserAddRows="False" EnableRowVirtualization="False" HorizontalAlignment="Stretch" HorizontalContentAlignment="Stretch" IsReadOnly="True" IsSynchronizedWithCurrentItem="True" ItemsSource="{Binding}" Name="dgrTextGroupText" VerticalContentAlignment="Stretch" Grid.Column="3" Grid.Row="1" Grid.RowSpan="6" CanUserReorderColumns="False" CanUserSortColumns="False">
    <DataGrid.Columns>
        <DataGridTextColumn Binding="{Binding Name}" Width="*" />
        <DataGridTextColumn Binding="{Binding Text}" Width="0" Visibility="Hidden" />
    </DataGrid.Columns>
    <DataGrid.RowStyle>
        <Style TargetType="{x:Type DataGridRow}">
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self}, Path=DataContext.text}" />
                </Trigger>
            </Style.Triggers>
        </Style>
    </DataGrid.RowStyle>
</DataGrid>

0

我需要根据单元格内容动态设置工具提示。我使用工具提示来显示单元格中的文本溢出。下面的绑定来自一个名为CellText的C#类属性。感谢上面的帖子,让我避免了自己找出整个过程的麻烦。

<DataGridTextColumn Header="HeaderText" Binding="{Binding DisplayText, Mode=OneWay}" Width="33*">
                            <DataGridTextColumn.CellStyle>
                                <Style>
                                    <Setter Property="ToolTipService.ToolTip" Value="{Binding DisplayText, Mode=OneWay}"/>
                                </Style>
                            </DataGridTextColumn.CellStyle>
                        </DataGridTextColumn>

0

不需要使用 ControlTemplate。如果你想在 ToolTip 中放置一个 StackPanel,只需将其设置为:

<Setter Property="ToolTip">
    <Setter.Value>
        <StackPanel>
            <TextBlock>txt1</TextBlock><TextBlock>txt2</TextBlock>
        </StackPanel>
    </Setter.Value>
</Setter>

10
你会认为这样做可以,但这是不行的。会出现异常错误:无法将类型为“System.Windows.Controls.StackPanel”的内容添加到类型为“System.Object”的对象中。在标记文件“NewsCluesWpf;component/processdictionary.xaml”的第31行第31个位置的对象“System.Windows.Controls.StackPanel”处出现错误。 - Sean Turner

0

不确定您是否可以通过XAML完成它。

更简单的方法可能是只需处理LoadingRow事件。在XAML中,可以像这样编写:

<dg:DataGrid Name="dgResults" AutoGenerateColumns="True" 
             LoadingRow="dgResults_LoadingRow" 
             ItemsSource="{Binding ListOfStrings}" />

然后在代码后台

void dgResults_LoadingRow(object sender, DataGridRowEventArgs e)
{
    DataGridRow row = e.Row;
    row.ToolTip = row.DataContext as string;
}

显然,您需要根据在数据网格中填充数据的方式更改代码。这也没有经过测试 =)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接