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
24.34k stars 2.1k forks source link

MenuFlyout ItemsSource item binding command always disabled #15689

Open 29654761 opened 1 month ago

29654761 commented 1 month ago

Describe the bug

After I upgrade to 11.1.0-beta2, all my menu items are disabled.

To Reproduce

<Button ToolTip.Tip="摄像头">
    <Image Source="avares:/Assets/Images/btn_camera.png" />
    <Button.Flyout>
        <MenuFlyout ItemsSource="{Binding VideoDevItems}" Opening="MenuVideo_Opening" />
    </Button.Flyout>
    <Button.Styles>
        <Style Selector="MenuItem" x:DataType="models:VideoDeviceViewModel">
            <Setter Property="Header" Value="{Binding Text}"/>
            <Setter Property="Command" Value="{Binding #PART_Frame.SelectVideoDevice}" />
            <Setter Property="CommandParameter" Value="{Binding}" />
            <Setter Property="Icon">
                <Template>
                    <Image Source="avares:/Assets/Images/checked.png" IsVisible="{Binding IsSelected}" />
                </Template>
            </Setter>
        </Style>
    </Button.Styles>
</Button>

The menu items are always disabled.

If I remove

<Setter Property="Command" Value="{Binding #PART_Frame.SelectVideoDevice}" />

MenuItem will be enabled.

This code is work fine on 11.0.10,

Expected behavior

No response

Avalonia version

11.1.0-beta2

OS

Windows

Additional context

No response

grokys commented 4 weeks ago

Hi @29654761 - I've tried to reproduce your problem but unfortunately I don't have enough information to be able to do so. For example, the #PART_Frame control that is the source of your command isn't included in your example code.

Would you be able to create a minimal repro in the form of a GitHub repository or a .zip file that I can run?

In addition are you seeing any binding errors being logged?

29654761 commented 3 weeks ago

I Created a new project and copy code, but it works ok. The raw project is created from old version, I tried to figure out the diffence, I found out remove the below line in csproj file can work fine.

<AvaloniaUseCompiledBindingsByDefault>true</AvaloniaUseCompiledBindingsByDefault>
timunie commented 3 weeks ago

@29654761 that means you are having a DataContext issue most likely. You may need to cast or sprcify x:DataType

See: https://docs.avaloniaui.net/docs/basics/data/data-binding/compiled-bindings

[!NOTE] If you upload a minimal sample and we verify it is an issue, I'll reopen this issue.

29654761 commented 3 weeks ago

AvaloniaApplication1.zip

@timunie This minimal sample can reproduce the issue.

@29654761 that means you are having a DataContext issue most likely. You may need to cast or sprcify x:DataType

See: https://docs.avaloniaui.net/docs/basics/data/data-binding/compiled-bindings

Note

If you upload a minimal sample and we verify it is an issue, I'll reopen this issue.

rabbitism commented 3 weeks ago

image

29654761 commented 3 weeks ago

AvaloniaApplication1.zip Sorry , I uploaded again.

timunie commented 2 weeks ago

The repo is great. Points out, it works if:

No clue why, but maybe @grokys has an idea where to look at. No priorty issue, as we have enough ways to make the same thing working.