© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•2y ago•
76 replies
stigzler

✅ How to use IRelayCommand in CommunityToolkit.Mvvm with parameters?

I'm just learning WPF and also the community toolkit. I can get RelayCommands working OK if they're parameter-less, but struggling to implement a command bound to a Toggle Button to toggle a bool property on a databound control. My working approach to simple commands:

public IRelayCommand LogOutCMD { get; set; }
private void SetupCommands() // this is called at startup
{
    LogOutCMD = new RelayCommand(LogOut);
}
private void LogOut()
{
  // does stuff
}
public IRelayCommand LogOutCMD { get; set; }
private void SetupCommands() // this is called at startup
{
    LogOutCMD = new RelayCommand(LogOut);
}
private void LogOut()
{
  // does stuff
}

However, I have tried something similar for the ToggleButton:
public bool CodeNumberingVisible { get => codeNumberingVisible; set => SetProperty(ref codeNumberingVisible, value); }
public IRelayCommand SetCodeNumberingVisibleCMD { get; set; }
SetCodeNumberingVisibleCMD = new RelayCommand<bool>(SetCodeNumberingVisible);
private void SetCodeNumberingVisible(bool visible)
{
    CodeNumberingVisible = visible;
}
public bool CodeNumberingVisible { get => codeNumberingVisible; set => SetProperty(ref codeNumberingVisible, value); }
public IRelayCommand SetCodeNumberingVisibleCMD { get; set; }
SetCodeNumberingVisibleCMD = new RelayCommand<bool>(SetCodeNumberingVisible);
private void SetCodeNumberingVisible(bool visible)
{
    CodeNumberingVisible = visible;
}

The button and control xaml:
<ToggleButton x:Name="CodeNumberVisibleBT" ToolTip="Toggle Code Numbers"
                Command="{Binding SetCodeNumberingVisibleCMD}"
                 IsChecked="{Binding CodeNumberingVisible}"
                 CommandParameter="{Binding IsChecked, RelativeSource={RelativeSource Self}}"/>
<syncfusion:EditControl ShowLineNumber="{Binding CodeNumberingVisible}"/>
<ToggleButton x:Name="CodeNumberVisibleBT" ToolTip="Toggle Code Numbers"
                Command="{Binding SetCodeNumberingVisibleCMD}"
                 IsChecked="{Binding CodeNumberingVisible}"
                 CommandParameter="{Binding IsChecked, RelativeSource={RelativeSource Self}}"/>
<syncfusion:EditControl ShowLineNumber="{Binding CodeNumberingVisible}"/>


However, this renders the button inoperable (no rollover highlighting, command doesn't execute) - it works without the Command Binding.

Any ideas?

EDIT: I've just realised that I don't technically need a Command here, I can just bind the ToggleButton's IsChecked property to the property directly. However, I'm still intersted to know why the above broke the ToggleButton and how to send parameters via cmmands using the Community Toolkit.
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements
Next page

Similar Threads

How to Properly Synchronize ViewModel and Model in MVVM (CommunityToolkit.Mvvm)
C#CC# / help
11mo ago
❔ ✅ Databinding using CommunityToolkit.MVVM
C#CC# / help
3y ago
CommunityToolkit.MVVM ObservableProperty and JsonIgnore
C#CC# / help
15mo ago
✅ How to use Dapper with Query Parameters
C#CC# / help
2y ago