npolyak / NP.Ava.UniDock

New (Avalonia 11) UniDock repository
MIT License
141 stars 10 forks source link

TreeDataGrid inside DockItem gives InvalidCastException when moving dock #12

Closed cirrusone closed 6 months ago

cirrusone commented 7 months ago

I get the following error when moving a DockItem containing a TreeGridView. The TreeGridView displays fine inside the DockItem but the error is raised only when attempting to dock.

Error:

System.InvalidCastException
  HResult=0x80004002
  Message=Unable to cast object of type 'NP.Ava.UniDock.DockItem' to type 'DataGridAot.ViewModels.MainViewModel'.
  Source=DataGridAot
  StackTrace:
   at CompiledAvaloniaXaml.XamlIlHelpers.DataGridAot.ViewModels.MainViewModel,DataGridAot.Source!Getter(Object )

  This exception was originally thrown at this call stack:
    [External Code]

MainView.axaml:

<!-- Dock -->
<np:RootDockGroup Grid.Row="1" DockId="RootGroup" np:DockAttachedProperties.TheDockManager="{StaticResource TheDockManager}">
    <np:TabbedDockGroup TabStripPlacement="Top">
        <np:DockItem Header="LeftTop 1"
             DockId="LeftTop1"
             CanClose="False">
            <TreeDataGrid Source="{Binding Source}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>
        </np:DockItem>
        <np:DockItem Header="LeftTop 2"
             DockId="LeftTop2"
             CanClose="False">
            <TextBlock Text="Left Top 2"
                     HorizontalAlignment="Center"
                     VerticalAlignment="Center"/>
        </np:DockItem>
    </np:TabbedDockGroup>
</np:RootDockGroup>

Full sample project with error -> DataGridAot.zip

npolyak commented 7 months ago

I'll take a look on Sunday Feb 11th. Sorry, just noticed it.

cirrusone commented 7 months ago

I noticed something else with this...

If I manually set the grid source in code-behind using this instead...

<TreeDataGrid x:Name="treeDataGrid" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>

and

public partial class MainView : UserControl
{
    public MainView()
    {
        InitializeComponent();

        var people = new List<Person>
        {
            new Person { Forename = "Forename1", Surname = "Surname1", Age = 32 },
            new Person { Forename = "Forename2", Surname = "Surname2", Age = 74 },
            new Person { Forename = "Forename3", Surname = "Surname3", Age = 16 },
            new Person { Forename = "Forename4", Surname = "Surname4", Age = 52 }
        };
        People = new ObservableCollection<Person>(people);

        Source = new FlatTreeDataGridSource<Person>(People)
        {
            Columns =
            {
                new TextColumn<Person, string>("Forename", x => x.Forename),
                new TextColumn<Person, string>("Surname", x => x.Surname),
                new TextColumn<Person, int>("Age", x => x.Age),
            },
        };

        treeDataGrid.Source = Source;
    }

    public ObservableCollection<Person> People { get; set; }
    public FlatTreeDataGridSource<Person> Source { get; set; }
}

then the DockItem tabs can be moved without it crashing.

The crash only occurs when setting source via MVVM binding to viewmodel using

<TreeDataGrid Source="{Binding Source}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>

npolyak commented 7 months ago

Sorry, I should have asked you before - can you create a small sample that compiles and runs in some public repository on github. I'll make it work then. Thanks

cirrusone commented 7 months ago

There's a link to full .sln/.proj with error in the initial post. Full sample project with error -> DataGridAot.zip

npolyak commented 6 months ago

First of all the UniDock still depends on Avalonia 6. I updated your references to only depend on Avalonia 6 and also also upgraded NP.Ava.UniDock to the latest version - 1.0.6 and everything was working fine - no exceptions.

The only problem is that your view model was tied to the MainView so no data appeared in the dragged out window. In order to bind the View Models correctly, please, see the end of the article UniDock Power Features starting with "Using View Models with the DockManager" section

npolyak commented 6 months ago

DataGridAot.zip Here is the modified code that runs without exceptions.

npolyak commented 6 months ago

please, close the issue, if you question was answered.

npolyak commented 6 months ago

Since there is no answer - closing this issue (apparently it happened because of the Avalonia dll mismatch.