C# GridViewColumn工具提示中未考虑样式
因此,我有以下问题: 在我的C# GridViewColumn工具提示中未考虑样式,c#,wpf,tooltip,C#,Wpf,Tooltip,因此,我有以下问题: 在我的列表视图中,我想将工具提示添加到特定的网格视图列。 有时这些工具提示是空的,我需要隐藏它们 当我在列表视图行上有工具提示时,在我的App.xaml文件中执行以下操作没有问题: <Style TargetType="ToolTip"> <Style.Triggers> <Trigger Property="Content" Value="{x:Static sys:String.Empty}"> &
列表视图
中,我想将工具提示
添加到特定的网格视图列
。
有时这些工具提示是空的,我需要隐藏它们
当我在列表视图
行上有工具提示
时,在我的App.xaml
文件中执行以下操作没有问题:
<Style TargetType="ToolTip">
<Style.Triggers>
<Trigger Property="Content" Value="{x:Static sys:String.Empty}">
<Setter Property="Visibility" Value="Collapsed" />
</Trigger>
<Trigger Property="Content" Value="{x:Null}">
<Setter Property="Visibility" Value="Collapsed" />
</Trigger>
</Style.Triggers>
</Style>
当工具提示为空时,如何隐藏工具提示?myApp.xaml
中的代码不起作用。
还尝试在代码隐藏中执行此操作:
TextBlock item = (TextBlock)sender;
ToolTip toolTip = (ToolTip)item.ToolTip;
但第二行给了我一个异常,作为项。工具提示
是一个堆栈面板
对象,无法转换?
事实上,我只在输入文本框
元素时才计算工具提示
内容,所以我想我会在此时检查并应用工具提示。可见性
,但我做不到。请尝试此代码
Please try this Code
<DataGridTextColumn Width="200" Binding="{Binding Name}">
<DataGridTextColumn.ElementStyle>
<Style TargetType="{x:Type TextBlock}" BasedOn="{StaticResource TextBlockDefaultStyle}">
<Setter Property="ToolTip" Value="{Binding Name}">
<Setter Property="ToolTipService.ShowDuration" Value="6000">
</Style>
</DataGridTextColumn.ElementStyle>
</DataGridTextColumn>
使用矩形代替工具提示
<GridViewColumn Header="{x:Static p:Resources.Name}" Width="100">
<GridViewColumn.CellTemplate>
<DataTemplate>
<Grid>
<TextBlock Text="{Binding Path=Name}" Tag="{Binding Name}"/>
<Rectangle Fill="Transparent" ToolTipService.ToolTip="{Binding Path=ToolTipModifications}" MouseEnter="UIElement_OnMouseEnter"/>
</Grid>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
如果将
TextBlock
的ToolTip
属性设置为如下所示,则您的样式应该可以工作:
<TextBlock Text="{Binding Path=Name}" Tag="{Binding Name}" MouseMove="mouseOverNameRepere"
ToolTip="{Binding Path=ToolTipModifications}" />
我想您不需要工具提示中的堆栈面板
和网格
,是吗?您可以将工具提示设置为null
,如果您将文本块
的工具提示
属性设置如下:
@dymanoid实际上,在该示例中,我不需要StackPanel和Grid,只需要放置一个TextBlock,StackPanel和Grid出现在这里只是因为我从另一个工具提示中获取了代码。我有两个答案,所以我现在就试试。你不仅应该提供一些代码片段,还应该解释为什么这是一个好的解决方案,@Rakesh Dhamejani事实上你的答案看起来非常类似于mm8注释更高,但代码更长,我不理解所有代码。非常感谢,我还看了另外两个答案,并确认你的答案远不是最简单的。谢谢(只需看看Icecat的建议,这是一个有趣的解决方案,但比你的更复杂)。解决方案也很有效。谢谢,不好,我只能选择好的答案,(在我看来)mm8更好,因为它更容易,我可以自动应用我的风格。无论如何,谢谢你抽出时间
private void UIElement_OnMouseEnter(object sender, MouseEventArgs e)
{
if (sender is Rectangle rectangle)
{
if (string.IsNullOrEmpty(rectangle.ToolTip.ToString()))
{
rectangle.Visibility = Visibility.Collapsed;
}
}
}
<TextBlock Text="{Binding Path=Name}" Tag="{Binding Name}" MouseMove="mouseOverNameRepere"
ToolTip="{Binding Path=ToolTipModifications}" />