Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7509847
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T23:02:54+00:00 2026-05-29T23:02:54+00:00

I am trying to bind a RelayCommand’s CanExecute in my main window to a

  • 0

I am trying to bind a RelayCommand’s CanExecute in my main window to a child window that possibly does not exist. How should I do it?

Currently I have:

<MenuItem Header="_Compact" 
       Command="{Binding Path=CurrentChildViewModel.CompactCommand}"
       IsEnabled="{Binding CurrentChildViewModel.CanExecuteCompactCommand, 
        Converter={StaticResource NullToBooleanConverter}}"/>

However this does not seem to work because the converter should work on CurrentChildViewModel (and not the CanExecuteCompactCommand, but I also should include that CanExecuteCompactCommand somehow.

I want the menu item to be enabled only if CurrentChildViewModel != null and CurrentChildViewModel.CanExecuteCompactCommand() returns true.

(reason: the CurrentChildViewModel is a window’s ViewModel that can be opened or not, if it is not opened, I want the menu item to be disabled. And if it is opened, I want the Compact command’s CanExecute method to check if the compact command can be executed, which is something like at least two items in the listview in the ChildView (Model) are selected.)

Can anybody help please?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T23:02:55+00:00Added an answer on May 29, 2026 at 11:02 pm

    if your converter need the instance of CurrentChildViewModel then bind to that and not the command (remove .CanExecuteCompactCommand)
    That said why on earth are you using one command to determine if another command should be able to execute? You should utilize the CanExecute of your command (CompactCommand).


    Ok I think I understand your actual problem now.
    If I’m correct then your xaml/bindings work as expected unless either CurrentChildViewModel or CanExecuteCompactCommand is null. (assuming you remove your converter.)

    To solve this you can add FallbackBalue=false to your binding, this tells the binding to use false when it cannot find the source. And also add TargetNullValue=false this tells the binding to use false when the source is null (CompactCommand in this case)

    So it would look like:

    IsEnabled="{Binding CurrentChildViewModel.CanExecuteCompactCommand,
                        FallbackValue=false,
                        TargetNullValue=false}"
    

    That said I still would discourage the usage of a command to determine if another command can execute. I would do would do something like this:

    e.g.

    <Style TargetType="{x:Type MenuItem}" x:Key="menuItemWithCommand">
        <Style.Triggers>
            <Trigger Property="Command" value="{x:Null}">
                <Setter Property="IsEnabled" Value="False"/>
            </Trigger>
        </Style.Triggers>
    </Style>
    ...
    <MenuItem Header="_Compact"
              Style="{StaticResource menuItemWithCommand}"
              Command="{Binding Path=CurrentChildViewModel.CompactCommand}" />
    ...
    CompactCommand= new RelayCommand(CompactCommandExecuted, CompactCommandCanExecute);
    private void CompactCommandExecuted(obejct obj)
    {   // Do your business
    }
    private bool CompactCommandCanExecute(object obj)
    {
        // return true if the command is allowed to be executed; otherwise, false.
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to bind column visibility to that of another element like this: <Window
I am trying to bind model received from Ajax call but that do not
I'm trying to bind a XAML ComboBox so that its list items are the
I am trying to bind a context menu item to a command that I
Trying to bind a String to a RichTextBox.Text property so that when the String
I'm trying to bind an event to the touchstart, touchmove, and touchend properties that
I'm trying to bind an ObservableCollection to a treeview in WPF. It does kind-of
I'm trying to bind a gridview to a linq to sql query that's using
Im currently trying to bind the DataGrid.ItemsSource to a custom RowCollection, which implements IList
I am trying to bind a userControl that I create in a class as

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.