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

Support nested types in DataTemplate.DataType #2725

Closed
FoggyFinder opened this issue Jul 9, 2019 · 14 comments · Fixed by kekekeks/XamlX#122
Closed

Support nested types in DataTemplate.DataType #2725

FoggyFinder opened this issue Jul 9, 2019 · 14 comments · Fixed by kekekeks/XamlX#122

Comments

@FoggyFinder
Copy link
Contributor

FoggyFinder commented Jul 9, 2019

Currently TypeResolver goes crazy - raises exception (for 0.8.x version [clarification: only for F# modules that don't have namespace]) and gives error in compile time from the master branch.

Just to show some sample. This kind of code is perfectly valid in WPF:

        <ItemsControl ItemsSource="{Binding Items}">
            <ItemsControl.ItemTemplate>
                <DataTemplate DataType="{x:Type local:SomeClass+Data}">
                    <TextBlock Text="{Binding Value}" />
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            DataContext = this;
        }

        public IEnumerable<Data> Items { get => Enumerable.Range(0, 10).Select(_ => new Data()); }
    }

    public static class SomeClass
    {
        public class Data
        {
            public string Value { get; } = "Hello World";
        }
    }

While this is a rare use-case for C# in F# world it's common. Instead (or additional to) of using namespace, types are grouped in modules, like this:

module Test = 
    type SomeType = { Id : int; Value : string }

So I think it would be very nice to have support for this scenario.

@FoggyFinder
Copy link
Contributor Author

Slightly changed code:

  <ItemsControl Items="{Binding Items}">
    <ItemsControl.ItemTemplate>
      <DataTemplate DataType="{x:Type local:SomeClass+Data}">
        <TextBlock Text="{Binding Value}" />
      </DataTemplate>
    </ItemsControl.ItemTemplate>
  </ItemsControl>

is works in 0.8 but doesn't for latest version (0.8.999-cibuild0003188-beta).

@kekekeks kekekeks self-assigned this Jul 15, 2019
@kekekeks kekekeks added this to the 0.9 milestone Jul 15, 2019
@FoggyFinder
Copy link
Contributor Author

Any updates?

@FoggyFinder
Copy link
Contributor Author

Any news? :-)

@BAndysc
Copy link
Contributor

BAndysc commented Dec 19, 2021

+1, it prevents from using CompiledBindings in few places :(

@grokys
Copy link
Member

grokys commented Jan 13, 2022

Investigating this now, looks like this works fine in unit tests, which makes it harder to fix...

Update: yep, seems to be our Mono.Cecil backend that's not handling this correctly.

grokys added a commit to kekekeks/XamlX that referenced this issue Jan 13, 2022
grokys added a commit that referenced this issue Jan 13, 2022
Including a repro (`EnumToEnumerable`) from a customer. Looks like the problem can't be reproduced in unit tests.
grokys added a commit that referenced this issue Jan 13, 2022
@maxkatz6 maxkatz6 modified the milestones: 0.9, 11.0 Jun 19, 2022
@maxkatz6 maxkatz6 modified the milestones: 11.0, 11.x Jul 5, 2023
@yankun
Copy link
Contributor

yankun commented Sep 5, 2023

Using 11.0.4 I've auto completion for the + syntax, but getting a compile error that the type can't be found. Still not solved?

@timunie
Copy link
Contributor

timunie commented Sep 6, 2023

@yankun issue is open and milestone is 11.x, so guess, no it's not available yet and doesn't have high priority seems like.

@nuc134r
Copy link

nuc134r commented Oct 4, 2023

I have written a replacement x:Static markup extension which supports nested classes. It uses WPF code. Can be used in the meantime

https://gist.github.com/nuc134r/a28efe6363ac5228df7d12d97af03f8d

Just remove the x: in your code and let autocomplete import it's XAML namespace (wherever you put it) and you're ready to go

Didn't look into x:Type but maybe it can be worked around in the same way

@timunie
Copy link
Contributor

timunie commented Oct 4, 2023

@nuc134r if you have an idea how to properly support this, consider to file a PR instead of relying on a workaround. Community support is appreciated as far as I can tell.

@nuc134r
Copy link

nuc134r commented Oct 4, 2023

@nuc134r if you have an idea how to properly support this, consider to file a PR instead of relying on a workaround. Community support is appreciated as far as I can tell.

It looks like PRs which seem to exactly (and properly) fix the issue are already filed

#7354
kekekeks/XamlX#54

@maxkatz6
Copy link
Member

maxkatz6 commented Oct 4, 2023

@nuc134r keep in mind, that this workaround will use reflection, and these static members might be trimmed out from the assembly if they are not used compile time.

@Dragorn421
Copy link
Contributor

Just to make sure, is the current state of things in Avalonia that you cannot use a nested class to set DataType explicitly?

@DmitriiShamrikov
Copy link

I'm using Avalonia 11.2.3 but this is still not working. Any idea when the fix going to be published?

@timunie
Copy link
Contributor

timunie commented Jan 21, 2025

if 11.2 doesn't have it, please try nightly, In this case should be shipped with 11.3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment