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
26.1k stars 2.26k forks source link

Button becomes grayed out when command is added to it #3242

Closed Avngarde closed 5 years ago

Avngarde commented 5 years ago

Hello! There's an issue i couldn't find solution for. When i add a command to a button it becomes grayed out, and it's not click able.

<Button Width="80" Height="50" HorizontalAlignment="Right" VerticalAlignment="Center" IsEnabled="True" Margin="0,0,10,0" Command="{Binding OnButtonClick}">Play now</Button>

private void OnButtonClick(object sender, RoutedEventArgs e) { Console.WriteLine("Played!"); }

maxkatz6 commented 5 years ago

I am not sure about Avalonia, but in WPF and UWP Binding doesn't support event handlers as Command value (except x:Bind in UWP, but it is special). So you need use just Click="OnButtonClick" without commands.

Or create property of type ICommand, for example:

public ICommand ButtonClickCommand { get; }
Command="{Binding ButtonClickCommand}"

You should also implement ICommand or find some MVVM framework with it. Something like ReactiveUI.

Avngarde commented 5 years ago

So i should rather implement the command in ViewModel with ReactiveUI than in view?

maxkatz6 commented 5 years ago

@ShamanDev for MVVM - yes, commands should be defined in ViewModel.

Avngarde commented 5 years ago

I think i found the real issue here. Command makes my button grayed out when it's part of ItemsPresenter, yet implementing command in ViewModel with ReactiveUI works for buttons outside of it.

jpgarza93 commented 8 months ago

Can we reopen this? I have the same issue. I am using MvvmToolkit to create my commands.

ViewModel c#

[RelayCommand]
private async Task StartRecorderAsync()

and then in xaml

<Button Content="Start" Command="{Binding StartRecorderCommand}"/>

This will result in a grayed-out button in the VS22 UI preview

timunie commented 8 months ago

Use CompiledBindings

jpgarza93 commented 8 months ago

I do use compile bindings.

my current workaround to enable buttons in UI preview is to add Priority like this:

<Button Content="Start" Command="{Binding StartRecorderCommand, Priority=1}"/>
timunie commented 8 months ago

Please open a new Q&A with a sample attached. I'll not reopen such an old issue, must be something similar but not the exact same thing. Thx.