【问题标题】:How to specify a ToolTip for a control in a Style from XAML?如何从 XAML 中为样式中的控件指定工具提示?
【发布时间】:2010-10-22 05:31:27
【问题描述】:

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

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

我想我需要设置工具提示的“模板”属性,但我似乎不知道该怎么做...

  <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 分钟处理这个问题……我很想知道为什么会这样……

标签: wpf datagrid tooltip


【解决方案1】:

不需要ControlTemplate。如果你想要ToolTip中的StackPanel,只需将其设置为:

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

【讨论】:

  • 你会这么想,但这行不通。给出例外:无法将“System.Windows.Controls.StackPanel”类型的内容添加到“System.Object”类型的对象。标记文件“NewsCluesWpf;component/processdictionary.xaml”第 31 行位置 31 中的对象“System.Windows.Controls.StackPanel”出错。
【解决方案2】:

不确定是否可以通过 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;
}

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

【讨论】:

    【解决方案3】:

    想通了...花了我大约 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>
    

    谢谢!

    【讨论】:

      【解决方案4】:

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

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

      【讨论】:

        【解决方案5】:

        我需要根据单元格内容动态设置工具提示。我正在使用工具提示来显示单元格中的文本溢出文本。下面的绑定来自名为 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>
        

        【讨论】:

          【解决方案6】:

          我还通过一些更改来实现此功能;包括它可以帮助某人。

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

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2023-03-22
            • 2019-05-09
            相关资源
            最近更新 更多