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

MenuBar Command x:Bind / Binding does not work #10479

Closed
jarsulko opened this issue Nov 17, 2022 · 3 comments · Fixed by #10517
Closed

MenuBar Command x:Bind / Binding does not work #10479

jarsulko opened this issue Nov 17, 2022 · 3 comments · Fixed by #10517
Labels
area/xBind 🪢 Categorizes an issue or PR as relevant to x:Bind difficulty/tbd Categorizes an issue for which the difficulty level needs to be defined. kind/bug Something isn't working project/binding 🪢 Categorizes an issue or PR as relevant to the binding engine

Comments

@jarsulko
Copy link

Current behavior

Command bindings are not working properly in MenuBar>MenuBarItem>MenuFlyoutItem:

  • (1) when using x:Bind the Commands are executed, but MenuFlyoutItem IsEnabled state is not updated according to CanExecute (or to be precise it is updated only once).
  • (2) when using Binding the Commands are not working completely (nothing happends).

Command bindings are not working properly in Button>MenuFlyout>MenuFlyoutItem:

  • (3) when using x:Bind the Commands are executed, but MenuFlyoutItem IsEnabled state is not updated according to CanExecute (or to be precise it is updated only once).
  • (4) when using Binding everything works it should (including CanExecute).

The issue occurs in Skia.Wpf and Skia,Gtk (at least).
Tested also on Android: x:Bind works the same way, Binding is working correctly also in scenario (2).

Expected behavior

Command x:Bind and Binding works correctly in MenuBar>MenuBarItem>MenuFlyoutItem on Skia.Wpf and Skia.Gtk.

How to reproduce it (as minimally and precisely as possible)

MainPage.xaml (Page content):

<StackPanel>
    <TextBlock Text="MenuBar+MenuBarItem+MenuFlyoutItem" />
    <MenuBar>
        <MenuBarItem Title="x:Bind Commands">
            <MenuFlyoutItem Text="MenuItem1" Command="{x:Bind _viewModel.Item1Command, Mode=OneWay}" />
            <MenuFlyoutItem Text="MenuItem2" Command="{x:Bind _viewModel.Item2Command, Mode=OneWay}" />
        </MenuBarItem>
        <MenuBarItem Title="Binding Commands">
            <MenuFlyoutItem Text="MenuItem1" Command="{Binding Item1Command}" />
            <MenuFlyoutItem Text="MenuItem2" Command="{Binding Item2Command}" />
        </MenuBarItem>
    </MenuBar>
    <TextBlock Text="Button+MenuFlyout+MenuFlyoutItem:" />
    <StackPanel Orientation="Horizontal">
        <Button Content="x:Bind Commands">
            <Button.Flyout>
                <MenuFlyout Placement="Bottom">
                    <MenuFlyoutItem Text="MenuItem1" Command="{x:Bind _viewModel.Item1Command, Mode=OneWay}" />
                    <MenuFlyoutItem Text="MenuItem2" Command="{x:Bind _viewModel.Item2Command, Mode=OneWay}" />
                </MenuFlyout>
            </Button.Flyout>
        </Button>
        <Button Content="Binding Commands">
            <Button.Flyout>
                <MenuFlyout Placement="Bottom">
                    <MenuFlyoutItem Text="MenuItem1" Command="{Binding Item1Command}" />
                    <MenuFlyoutItem Text="MenuItem2" Command="{Binding Item2Command}" />
                </MenuFlyout>
            </Button.Flyout>
        </Button>
    </StackPanel>
    <TextBlock Text="x:Bind:" />
    <TextBlock Text="{x:Bind _viewModel.State, Mode=OneWay}" />
    <TextBlock Text="Binding:" />
    <TextBlock Text="{Binding State}" />
</StackPanel>

MainPage.xaml.cs

public sealed partial class MainPage : Page
{
    MainPageViewModel _viewModel;
    public MainPage()
    {
        this.InitializeComponent();
        _viewModel = new MainPageViewModel();
        DataContext = _viewModel;
    }
}

MainPageViewModel.cs (using CommunityToolkit.Mvvm)

using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;

[INotifyPropertyChanged]
public partial class MainPageViewModel
{
    [ObservableProperty]
    [NotifyCanExecuteChangedFor(nameof(Item1Command))]
    bool _canRunItem1 = true;

    [RelayCommand(CanExecute = nameof(CanRunItem1))]
    void Item1()
    {
        CanRunItem1 = false;
        CanRunItem2 = true;
        OnPropertyChanged(nameof(State));
    }

    [ObservableProperty]
    [NotifyCanExecuteChangedFor(nameof(Item2Command))]
    bool _canRunItem2 = false;

    [RelayCommand(CanExecute = nameof(CanRunItem2))]
    void Item2()
    {
        CanRunItem2 = false;
        CanRunItem1 = true;
        OnPropertyChanged(nameof(State));
    }

    public string State => $"CanRunItem1:{CanRunItem1}\nCanRunItem2{CanRunItem2}";
}

Workaround

Can use scenario (4): Button>MenuFlyout>MenuFlyoutItem and Binding.

Works on UWP/WinUI

Yes

Environment

Uno.WinUI / Uno.WinUI.WebAssembly / Uno.WinUI.Skia

NuGet package version(s)

No response

Affected platforms

Android, Skia (WPF), Skia (GTK on Linux/macOS/Windows)

IDE

Visual Studio 2022

IDE version

17.4.1

Relevant plugins

No response

Anything else we need to know?

I would be happy also with any clue where can I look at to analyze the issue and probably help to fix that.
Additional topic that I have noticed is that MenuFlyouts (but also Flyouts) have vertical ScrollBars visible in Skia.

@jarsulko jarsulko added difficulty/tbd Categorizes an issue for which the difficulty level needs to be defined. kind/bug Something isn't working triage/untriaged Indicates an issue requires triaging or verification labels Nov 17, 2022
@jeromelaban
Copy link
Member

Thanks for the report! This is indeed a curious behavior.

Could you attach all this in a sample repro app? Sometimes the behavior is related to the environment, and we want to get this part out of the way when reproducing what you're seeing.

@jeromelaban jeromelaban added area/xBind 🪢 Categorizes an issue or PR as relevant to x:Bind project/binding 🪢 Categorizes an issue or PR as relevant to the binding engine and removed triage/untriaged Indicates an issue requires triaging or verification labels Nov 17, 2022
@jarsulko
Copy link
Author

VS solution attached.
MenuBarCommandBinding.zip

@jeromelaban
Copy link
Member

Thanks, the repro helped!

Here's a wrap-up of three issues in the sample:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/xBind 🪢 Categorizes an issue or PR as relevant to x:Bind difficulty/tbd Categorizes an issue for which the difficulty level needs to be defined. kind/bug Something isn't working project/binding 🪢 Categorizes an issue or PR as relevant to the binding engine
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants