Created
March 8, 2023 13:19
-
-
Save danipen/d2f5c19a66a7f24048f510f080db1c9f to your computer and use it in GitHub Desktop.
Repro case for https://github.com/AvaloniaUI/Avalonia/issues/10598
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using Avalonia; | |
using Avalonia.Controls; | |
using Avalonia.Controls.Templates; | |
using Avalonia.Markup.Xaml; | |
using Avalonia.Media; | |
using Avalonia.Media.TextFormatting; | |
namespace ReproCase | |
{ | |
public partial class MainWindow : Window | |
{ | |
public MainWindow() | |
{ | |
this.AttachDevTools(); | |
AvaloniaXamlLoader.Load(this); | |
ListBox listBox = new ListBox(); | |
listBox.DataTemplates.Add(new FuncDataTemplate<string>((_, _) => | |
new TextBlockAutoTooltip() | |
{ | |
TextTrimming = TextTrimming.CharacterEllipsis | |
}, true)); | |
List<string> items = new List<string>(); | |
for (int i = 0; i < 10000; i++) | |
{ | |
if (i % 2 == 0) | |
{ | |
items.Add(string.Format("Item number {0} which needs a tooltip because it's long text", i)); | |
} | |
{ | |
items.Add("Short Item"); | |
} | |
} | |
listBox.Items = items; | |
this.Content = listBox; | |
} | |
public class TextBlockAutoTooltip : TextBlock | |
{ | |
protected override void OnDataContextChanged(EventArgs e) | |
{ | |
base.OnDataContextChanged(e); | |
if (DataContext == null) | |
return; | |
Text = DataContext.ToString(); | |
} | |
protected override TextLayout CreateTextLayout(string text) | |
{ | |
TextLayout result = base.CreateTextLayout(text); | |
SetupTooltipVisibility(result); | |
return result; | |
} | |
void SetupTooltipVisibility(TextLayout layout) | |
{ | |
if (TextTrimming == TextTrimming.None) | |
return; | |
bool showTooltip = !string.IsNullOrEmpty(Text) && | |
layout.TextLines.Any(line => line.HasCollapsed); | |
if (showTooltip) | |
ToolTip.SetTip(this, Text); | |
else | |
ToolTip.SetTip(this, null); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment