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

  • Home
  • SEARCH
  • 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 823429
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T02:56:45+00:00 2026-05-15T02:56:45+00:00

I’ll start off and say I am not using the MVVM pattern for my

  • 0

I’ll start off and say I am not using the MVVM pattern for my WPF app. Please forgive me.

Right now I have a data template with two buttons, each binds to a different command on the CLR object this data template represents. Both use the same command parameter. Here’s an example of the buttons.

<Button x:Name="Button1"
        Command="{Binding Path=Command1}"
        CommandParameter="{Binding Path=Text, ElementName=TextBox1}"
/>
<Button x:Name="Button2"
        Command="{Binding Path=Command2}"
        CommandParameter="{Binding Path=Text, ElementName=TextBox1}"
/>

I would like to refactor this into a single button that can perform either command based on a user setting like a boolean in Settings.settings. I don’t have access to refactoring the CLR object itself. Also this is a Data Template there isn’t codebehind for me to work with. My take is that a converter would be the best bet, but I don’t know how I would put that together.

The converter would need to take in the CommandParameter, as well as the DataContext so it knows which object to execute the Commands on.

Can someone lend me a hand with this? Thanks in advance.

  • 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-15T02:56:46+00:00Added an answer on May 15, 2026 at 2:56 am

    Quick and dirty solution – put both buttons on the form and bind their visibilities to the value of the bool (one negated, obviously) – that way only the valid button will be shown.

    If you do want to use a converter, though, I’d be inclined to use it on the command binding, pass in the CLR object and the value of the bool and allow it to return the right command to bind to. I see no reason to pass in the command parameter since that’ll be the same either way.

    XAML:

    <Resources>
      <controls:CommandConverter x:Key="CommandConverter"/>
    </Resources>
    
    <Button x:Name="Button" 
            CommandParameter="{Binding Path=Text, ElementName=TextBox1}">
        <Button.Command>
            <MultiBinding Converter="{StaticResource CommandConverter}">
              <MultiBinding.Bindings>
                <Binding /><-- the datacontext CLR object -->
                <Binding ... /><-- Application setting (however you intend to get that in)
              </MultiBinding.Bindings>
            </MultiBinding>
        </Button.Command>
    </Button>
    

    Code:

    public class CommandConverter : IMultiValueConverter
    {        
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            CommandObject clrObject= (CommandObject)values[0]; 
            bool setting = (bool)values[1];
    
            if (setting)
            {
                return clrObject.Command1;
            }
    
            return clrObject.Command2;
        }
    
        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
        {
            throw new NotSupportedException();
        }
    }
    

    Actually if the setting bool is just in the application settings you could just use it directly in the converter and only use a normal, single value converter. Not great design since it should really stand alone, but will get the job done.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.