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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/796290/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 20:18:31  来源:igfitidea点击:

How to specify a ToolTip for a control in a Style from XAML?

wpfdatagridtooltip

提问by Sean Turner

I'm using a the WPF datagrid from the Microsoft CodePlex project. I have a custom control that I want to databind to a field from the row of the datagrid. I can't for the life of me figure out how to specify a tooltip on a datagrid row.

我正在使用 Microsoft CodePlex 项目中的 WPF 数据网格。我有一个自定义控件,我想将其数据绑定到数据网格行中的一个字段。我一生都无法弄清楚如何在数据网格行上指定工具提示。

The closest I've come is to use a RowStyle with a Setter to set the tooltip, but this only seems to work for text. When I try to put a ControlTempalte in as the Value for the ToolTip, it displays the result of calling ToString on the ControlTemplate type.

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

I think I need to set the "Template" property of the ToolTip, but I can't seem to figure out how to do that...

我想我需要设置工具提示的“模板”属性,但我似乎无法弄清楚如何做到这一点......

  <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>

回答by Sean Turner

Figured it out... took me about 6 hours...

想通了......花了我大约6个小时......

For some reason, I can't set the value directly using Value.Setter. If I define the content for the tooltip as a static resource though, and then set it in the Style property of the DataGrid.RowStyle it works.

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

So, the datagrid row style looks like:

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

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

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

        </dg:DataGrid.RowStyle>

And the resource is

而且资源是

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

Thanks!

谢谢!

回答by TI82

The key is to use the Property ToolTipService.ToolTip, instead of ToolTip - like this:

关键是使用属性 ToolTipService.ToolTip,而不是 ToolTip - 像这样:

<Setter Property="ToolTipService.ToolTip" Value="My Tooltip"/>

回答by ValiantBoy

I also got this working with a couple of changes; included incase it helps someone.

我也做了一些改变;包括柜面它可以帮助某人。

My Datadrid is bound to a list of custom objects, I wanted to display the string "Name" as a column and the string "text" in the tooltip. The trick for me (newbe) was that I had to include the Text Column and hide it for it to show up in the tooltip, i.e.:

我的 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>

回答by sondlerd

I needed to set the tooltip dynamically based on the cell content. I'm using the tooltip to display text overflow text from the cell. The binding below is from a c# class property named CellText. Thanks to the posts above for allowing me to avoid figuring out the entire thing myself.

我需要根据单元格内容动态设置工具提示。我正在使用工具提示显示单元格中的文本溢出文本。下面的绑定来自名为 CellText 的 ac# 类属性。感谢上面的帖子让我避免自己弄清楚整个事情。

<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>

回答by Kent Boogaart

There's no need for the ControlTemplate. If you want the StackPanelin the ToolTip, just set it as:

不需要ControlTemplate. 如果你想在StackPanelToolTip,只需将其设置为:

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

回答by Jake Ginnivan

Not sure you can do it through XAML.

不确定您是否可以通过 XAML 做到这一点。

A easier way might be to just handle the LoadingRow event. In xaml have something like:

一种更简单的方法可能是仅处理 LoadingRow 事件。在 xaml 中有类似的东西:

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

Then in code behind

然后在后面的代码中

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

Obviously you will have to change the code depending on how you are populating the data in the datagrid. This is also untested =)

显然,您必须根据在数据网格中填充数据的方式来更改代码。这也是未经测试的 =)