Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ToolTips no longer use ViewLocator after updating to 11.1 #16665

Closed
airbreather opened this issue Aug 12, 2024 · 4 comments
Closed

ToolTips no longer use ViewLocator after updating to 11.1 #16665

airbreather opened this issue Aug 12, 2024 · 4 comments
Labels
by-design The behavior reported in the issue is actually correct.

Comments

@airbreather
Copy link

Describe the bug

I have an Avalonia application on 11.0 that uses the ViewLocator pattern. Sometimes, I set a ToolTip.Tip to something that's supposed to get its template that way.

When I upgraded to 11.1.2, these ToolTips stopped displaying at all.

To Reproduce

I managed to get a somewhat slim repro project and pushed it to: https://github.com/airbreather/AvaloniaRepro

When running this as-is using dotnet run (on my Linux desktop, which is all I have readily accessible at the moment), I get a big red rectangle with a ToolTip that reads "Testing 123 (in a tooltip!)".

If I replace all the 11.0.13 to 11.1.2 in AvaloniaRepro.csproj and run it again using dotnet run, I get that same big red rectangle but with a blank ToolTip.

Expected behavior

11.1 should behave the same as 11.0: the ToolTip should display "Testing 123 (in a tooltip!)".

Avalonia version

11.1.2

OS

Linux

Additional context

No response

@airbreather
Copy link
Author

Some progress in narrowing this down: this issue does not reproduce with the pared-down XAML-free version from this branch of that repository, and the relevant files are small enough for me to embed here:

AvaloniaRepro.csproj
<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>net8.0</TargetFramework>
    <Nullable>enable</Nullable>
    <BuiltInComInteropSupport>true</BuiltInComInteropSupport>
    <ApplicationManifest>app.manifest</ApplicationManifest>
    <AvaloniaUseCompiledBindingsByDefault>true</AvaloniaUseCompiledBindingsByDefault>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Avalonia" Version="11.1.2" />
    <PackageReference Include="Avalonia.Desktop" Version="11.1.2" />
  </ItemGroup>
</Project>
Program.cs
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.Presenters;
using Avalonia.Controls.Shapes;
using Avalonia.Controls.Templates;
using Avalonia.Data;
using Avalonia.Media;

Application app = AppBuilder.Configure<Application>()
    .UsePlatformDetect()
    .SetupWithClassicDesktopLifetime(args)
    .Instance!;

app.DataTemplates.Add(new ViewLocator());
Window window = new()
{
    Content = new Rectangle
    {
        Fill = Brushes.Red,
        [ToolTip.TipProperty] = new { Tip = "Testing 123" },
    },
};
window.Show();
app.Run(window);

public sealed class ViewLocator : IDataTemplate
{
    public bool Match(object? data) => data?.GetType().GetProperty("Tip") is not null;
    public Control Build(object? param) => new ContentPresenter
    {
        [!ContentPresenter.ContentProperty] = new Binding
        {
            Source = param,
            Path = "Tip",
            StringFormat = "{0} (from tooltip!)",
        },
    };
}

So it has something to do with the stuff that happens in the slightly fuller version that does not also happen here.

@airbreather
Copy link
Author

And here's another branch on 11.1 that also works around it, which confuses me. The change looks like:

 <UserControl xmlns="https://github.com/avaloniaui"
              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
              xmlns:avaloniaRepro="clr-namespace:AvaloniaRepro"
              x:DataType="avaloniaRepro:ToolTipViewModel"
-             x:Class="AvaloniaRepro.ToolTipView">
+             x:Class="AvaloniaRepro.ToolTipView"
+             x:Name="ThisControl">
-    <ContentPresenter Content="{Binding Tip, StringFormat='{}{0} (in a tooltip!)'}" />
+    <ContentPresenter Content="{ReflectionBinding DataContext.Tip, ElementName=ThisControl, StringFormat='{}{0} (in a tooltip!)'}" />
 </UserControl>

@TomEdwardsEnscape
Copy link
Contributor

This isn't related to tooltips, but is because your ToolTipView control is using ContentPresenter. That type is only for templates. The control's content should be ContentControl. and making this change fixes the issue.

The default template for UserControl generates its own ContentPresenter, the content of which is bound to UserControl.Content (i.e. the ContentPresenter of your ToolTipView, see screenshot below). This double presenter layer is presumably what breaks things. I don't know why it worked before.

image

@timunie timunie added by-design The behavior reported in the issue is actually correct. and removed regression bug labels Oct 27, 2024
@timunie
Copy link
Contributor

timunie commented Oct 27, 2024

Good catch @TomEdwardsEnscape . Yes, this is by design.

@timunie timunie closed this as not planned Won't fix, can't repro, duplicate, stale Oct 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
by-design The behavior reported in the issue is actually correct.
Projects
None yet
Development

No branches or pull requests

4 participants