AvaloniaUI / Avalonia

Develop Desktop, Embedded, Mobile and WebAssembly apps with C# and XAML. The most popular .NET UI client technology
https://avaloniaui.net
MIT License
25.34k stars 2.2k forks source link

DataContext inheritance issue #10242

Closed Seekeer closed 1 year ago

Seekeer commented 1 year ago

I have following code:

    <TabItem Header="Accounts"
                 d:DataContext="{Binding AccountsViewModelAccounts}"
                 >
            <StackPanel>
                <DataGrid AutoGenerateColumns="False" Name="accounts" Grid.Row="1"
                          Items="{Binding CollectionView}" >
                    <DataGrid.Columns>
                        <DataGridTextColumn Binding="{Binding Path=Name}" Header="Name" Width="150" IsReadOnly="True"/>
                    </DataGrid.Columns>
                </DataGrid>
            </StackPanel>
        </TabItem>

And got error. But when I move DataContext to grid I got items from AccountsViewModelAccounts.CollectionView

        <TabItem Header="Accounts"
                 >
            <StackPanel>
                <DataGrid AutoGenerateColumns="False" Name="accounts" Grid.Row="1"
                 d:DataContext="{Binding AccountsViewModelAccounts}"
                          Items="{Binding CollectionView}" >
                    <DataGrid.Columns>
                        <DataGridTextColumn Binding="{Binding Path=Name}" Header="Name" Width="150" IsReadOnly="True"/>
                    </DataGrid.Columns>
                </DataGrid>
            </StackPanel>
        </TabItem>

In WPF both piece of code will lead to the same results.

maxkatz6 commented 1 year ago

And got error.

What error?

In WPF both piece of code will lead to the same results.

Just to be clear, it's only a previewer related issue? As you are using design data context. Or the same issue with normal data context usage? Like, settings "DataContext" instead of "d:DataContext".

Seekeer commented 1 year ago

What error?

DataGrid is trying to bind to TabControl's DataContext.CollectionView not DataContext.AccountsViewModelAccounts.CollectionView

it's only a previewer related issue?

Nope. When I'm replacing it with plain DataContext issue remains the same.

maxkatz6 commented 1 year ago

@Seekeer if it's an error, please attach full message and stacktrace. If it's an error in the console log, please attach full error message. Ideally, minimal sample project would help.

Because, normally, this kind of code works just fine.

maxkatz6 commented 1 year ago

Duplicate of #10958 (closing this one, as another has simpler repro)