Open SparkyTD opened 1 year ago
I was having the same issue, and found that you can instead use ReflectionBinding for those specific cases. e.g. Instead of Command="{Binding ElementName=X, Path=DataContext.DoYCommand}" do Command="{ReflectionBinding ElementName=X, Path=DataContext.DoYCommand}"
This turns off compiled bindings for this binding only.
While I think in ReactiveWindow it should work without, one can also cast in the C# like cast style inside a binding. e.g.
{Binding $parent[Window].((vm:MyViewModel).DataContext).MyCommand }
I see this as well, and have the same use case as SparkyTD
亲: 您发的邮件已收到【自动回复】! 3Q,祝您生活愉快! VIC
I also have this issue in 0.10.19. Same use case as SparkyTD. @timunie 's suggestion of casting is a work around I'm using for now as it allows for the use of compiled bindings.
Describe the bug I'm experiencing an issue with Avalonia 11.0.0-rc1.1 where relative bindings fail to compile when
CompileBindings
is enabled. I've tried three different syntaxes for binding to the parent window's ViewModel, all of which yield the same error:{Binding ViewModel.TestMethodCommand, RelativeSource={RelativeSource AncestorType={x:Type views:MainWindow}}}
{Binding #Window.ViewModel.TestMethodCommand}
{Binding $parent[views:MainWindow].ViewModel.TestMethodCommand}
To Reproduce Steps to reproduce the behavior:
MainWindow
class derives fromReactiveWindow<MainWindowViewModel>
, and notWindow
CompiledBindings
toTrue
on the windowMainWindowViewModel
Expected behavior The program should compile successfully.
Desktop:
Additional context The error only occurs when
CompileBindings
is enabled. If I disable it, the code compiles and the binding works as expected. The error in question is an internal compiler error with aNullReferenceException
.The issue might be related to #10489 and #10486.
Use case The obvious answer would be to simply not use relative bindings and bind to the ViewModel directly (assuming that
x:DataType
is set on the control/window) which would work as expected regardless of the state ofCompiledBindings
. However in my specific use case, I was trying to bind to a command in a custom control's ViewModel from within theItemTemplate
of aListBox
(a simple "delete this row" button), where I had to use relative bindings to reach my ViewModel.This is not a critical bug in my case since I can just disable
CompiledBindings
for the time being, but I do enjoy the benefits of the function such as additional IntelliSense and syntax highlighting in the axaml editor when it's enabled, so I would consider it an annoying bug.