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

CommandParameters other than Strings are not acceptable for RelayCommands? #845

Open
1 of 4 tasks
MorElmaliach opened this issue Feb 25, 2024 · 0 comments
Open
1 of 4 tasks
Labels
bug 🐛 An unexpected issue that highlights incorrect behavior

Comments

@MorElmaliach
Copy link

MorElmaliach commented Feb 25, 2024

Describe the bug

Hello,

I'm trying to upgrade our code base from using MVVMLight to the CommunityToolkit, so far a part from specific small things it was smooth, but I've encountered a big issue during execution. Some of our buttons are bound to commands with a command parameter. These parameters are not always strings, some of them are bool ("False", "True"), some of them are int ("-1", "1"). For any commandparameter other than an explicit string with a fitting command, there's an exception thrown during runtime.

Code example from a test project I've made to isolate the issue:
<!--bar--> <StackPanel Grid.RowSpan="3" Grid.Column="0"> <Button Content="Increase" Width="70" Margin="15" Command="{Binding TestCommand}" CommandParameter="1"/> <Button Content="Decrease" Width="70" Margin="15" Command="{Binding TestCommand}" CommandParameter="-1"/> </StackPanel>

My viewmodel:
public ICommand TestCommand => new RelayCommand<int>((idx) => { _leftFocusRatio += idx; });
During runtime, once LoadComponent is executed and this part of the XAML is parsed, the exception is thrown:

System.ArgumentException: 'Parameter "parameter" (object) cannot be of type System.String, as the command type requires an argument of type System.Int32.
Parameter name: parameter'

I could not find any specific evidence in the existing or past issues, nor in the documentation for any limitations on the CommandParameters usable this way. Is this a bug or is this an issue?

Considered workarounds right now:

  • Changing all places we use a number or bool to a relaycommand with a string and then parse them. Problem with this approach is parameters we use in Interactivity EventToCommand actions which lead to the same crashes (it's not able to parse strings to event args).
  • Making commands for every interaction (increase command, decrease command, instead of one setcommand with a value parameter). Would double up our existing code and lead to bloat.

Regression

No response

Steps to reproduce

You can simply run the project attached. It's running on visual studio 17.6.5.
Otherwise, create a button with a command attached to it in the ViewModel.
Make the command a relaycommand with a template parameter of an int.
In the XAML add the attribute "CommandParameter" with a number, such as 1. 
Execute the project.

Expected behavior

I expect to be able to use binding to int or bool (or event args) parameters just as I did with MVVMLight before.
packages.zip

Screenshots

No response

IDE and version

VS 2022

IDE version

17.6.5

Nuget packages

  • CommunityToolkit.Common
  • CommunityToolkit.Diagnostics
  • CommunityToolkit.HighPerformance
  • CommunityToolkit.Mvvm (aka MVVM Toolkit)

Nuget package version(s)

8.2.2

Additional context

No response

Help us help you

No, just wanted to report this

@MorElmaliach MorElmaliach added the bug 🐛 An unexpected issue that highlights incorrect behavior label Feb 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐛 An unexpected issue that highlights incorrect behavior
Projects
None yet
Development

No branches or pull requests

1 participant