Prevent empty tooltips on wpf datagrid

I am working on a calendar program, which consists mainly of WPF DataGrid . Since there is not always enough space to display all the entries of the day (this is a DataGridCell ), a tooltip with all entries of the shell of the day appears when you hover over it. This still works with the code snippet shown below. And now the (small) problem: if there are no entries during the day, a pop-up shell pops up. Using the code below, a tooltip appears.

 <DataGridTemplateColumn x:Name="Entry" IsReadOnly="True"> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <Grid> <TextBlock Text="{Binding EntryText}" Foreground="{Binding EntryForeground}" FontWeight="{Binding EntryFontWeight}"> </TextBlock> <TextBlock Text="{Binding RightAlignedText}" Foreground="Gray" Background="Transparent"> <TextBlock.ToolTip> <TextBlock Text="{Binding AllEntriesText}"/> </TextBlock.ToolTip> </TextBlock> </Grid> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn> 

Data binding is done through

 myCalDataGrid.Itemssource = _listOfDays; 

in the code behind, where "Day" is the view model for one calendar line.

+6
source share
4 answers

As HB suggested linking directly to the ToolTip property instead of using a TextBlock, and if AllEntriesText is an empty string, you can apply a trigger on your TextBlock to disable your tooltip by setting the ToolTipService.IsEnabled property like this -

 <TextBlock Text="{Binding RightAlignedText}" Foreground="Gray" Background="Transparent" ToolTip="{Binding AllEntriesText}"> <TextBlock.Style> <Style TargetType="TextBlock"> <Style.Triggers> <Trigger Property="ToolTip" Value="{x:Static system:String.Empty}"> <Setter Property="ToolTipService.IsEnabled" Value="False" /> </Trigger> </Style.Triggers> </Style> </TextBlock.Style> </TextBlock> 

Be sure to add the namespace system in your xaml -

 xmlns:system="clr-namespace:System;assembly=mscorlib" 
+11
source

Bind directly to the ToolTip property (do not create a TextBlock for it) and set AllEntriesText to null , if there are no entries, then the ToolTip itself is also null and should not show.

+4
source

Thanks for the solutions, they can work, no question. But I need a TextBlock for a tooltip to format and align text. So I found this solution:

  <TextBlock Text="{Binding RightAlignedText}" HorizontalAlignment="Stretch" TextAlignment="Right" Padding="2,0" Foreground="Gray" Background="Transparent" ToolTipService.ShowDuration="60000" ToolTipService.BetweenShowDelay="0" ToolTipService.InitialShowDelay="0" > <TextBlock.ToolTip> <ToolTip Visibility="{Binding EntryToolTipVisibility}"> <TextBlock Text="{Binding ToolTipText}" TextAlignment="Left" FontFamily="Courier New"/> </ToolTip> </TextBlock.ToolTip> </TextBlock> 

I bound the Visibility property of the tooltip to the EntryToolTipVisibility rule (of type Visibility) in the view model. See code snippet below.

 public Visibility EntryToolTipVisibility { get { return _entries.Count > 0 ? Visibility.Visible : Visibility.Collapsed; } } 
+2
source

Another option is to use your own converter. I prefer this method, for example, for a TextBlock tooltip that displays TextBlock text, but for the case there is no text, an empty tooltip is not needed.

XAML Code:

 //step #1 xmlns:local="clr-namespace:MyNamespace" //step #2 - into Window.Resources or other <local:StringToVisibleTooltip x:Key="StringToVis" /> //step #3 - example of use <TextBlock ...other attributes... TextTrimming="CharacterEllipsis"> <TextBlock.ToolTip> <ToolTip Visibility="{Binding Path=Text, Converter={StaticResource StringToVis}}"> <TextBlock Text="{Binding Text}"/> </ToolTip> </TextBlock.ToolTip> </TextBlock> 

And the code for

  namespace MyNamespace { public class StringToVisibleTooltip : IValueConverter { public StringToVisibleTooltip() { } public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { if (value != null && ((string)value).Length > 0) //empty string does not need tooltip return Visibility.Visible; else return Visibility.Collapsed; } public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { return null; } } } 
0
source

Source: https://habr.com/ru/post/902131/


All Articles