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 554955
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T11:45:54+00:00 2026-05-13T11:45:54+00:00

I am having trouble with the MVVM pattern and Commands in my WPF app.

  • 0

I am having trouble with the MVVM pattern and Commands in my WPF app. The problem is not so much the MVVM pattern, but more the stuff that is going on on my GUI. I’ll explain the situation:

My app can DoStuff to some files. I have a class with a function DoStuff(int limit). My user user interface has the following items:

  • A Button DoStuffBtn to start parsing.
  • A TextBox LimitTxt to fill in a limit.
  • A CheckBox LimitChk to enabled or disable the limit.

When you would “uncheck” LimitChk, then LimitTxt.Text = "" and LimitTxt.IsEnabled = false. When you would “check” LimitChk, then LimitTxt.IsEnabled = false again, but the text remains empty until you fill something in.

I have read many tutorials on Commands in WPF and MVVM but I just can’t seem to pour my case into that mold. The example I gave is actually just a small part of my UI, but I can’t seem to do this nicely either.

I keep running into questions like:

  • Do I need two Commands for LimitChk (enable and disable) or just one (toggle)?
  • If I bind an int to LimitTxt, what happens if I make it empty and disable it?
  • Is it a clean way to just use DoStuff(Int32.Parse(LimitTxt.Text)) when DoStuffBtn is pressed?
  • If I use two commands on LimitChk, what happens with the CanExecute() function of ICommand that determines whether LimitChk is enabled?

So the main question is: How would the situation I described fit into a nice pattern using Commands in WPF?

Some links on WPF, Commands and MVVM i’ve looked at:

  • http://www.devx.com/DevX/Article/37893/0/page/1
  • http://msdn.microsoft.com/en-us/magazine/cc785480.aspx?pr=blog
  • http://jmorrill.hjtcentral.com/Home/tabid/428/EntryId/432/MVVM-for-Tarded-Folks-Like-Me-or-MVVM-and-What-it-Means-to-Me.aspx
  • http://msdn.microsoft.com/en-us/magazine/dd419663.aspx

What I understand so far is that I have to keep as much as possible out of the UI. Even stuff like UI influencing the UI. I.e. unchecking LimitChk disables LimitText. Still, I think I should keep a difference between UI related information and actions and stuff that actually has to do with the actual work that has to be done.

  • 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-13T11:45:54+00:00Added an answer on May 13, 2026 at 11:45 am

    Just some high level thoughts, leaving out superfluous stuff like Color and alignment attributes, WrapPanels, etc.

    Your ViewModel has a a couple properties:

    public bool? LimitIsChecked { get; set; }
    public bool LimitTextIsEnabled { get; set; }  //to be expanded, below
    public ICommand ParseCommand { get; private set; } // to be expanded, below
    public string LimitValue { get; set; } // further explanation, below
    

    Your XAML has CheckBox and TextBox definitions something like:

    <CheckBox Content="Limit Enabled" IsChecked="{Binding LimitIsChecked}" />
    <TextBox Text="{Binding LimitValue}" IsEnabled="{Binding LimitIsEnabled}" />
    <Button Content="Parse" Command="{Binding ParseCommand}" />
    

    You’ll want to initialize ParseCommand something like this:

    this.ParseCommand = new DelegateCommand<object>(parseFile);
    

    Now, let’s fill in that LimitTextIsEnabled property too:

    public bool LimitTextIsEnabled {
        // Explicit comparison because CheckBox.IsChecked is nullable.
        get { return this.LimitIsChecked == true; }
        private set { }
    }
    

    Your parseFile method would then pass the value of the LimitValue property to the logic doing the actual parsing.

    I declared the LimitValue property as string here to avoid cluttering up the code with an explicit converter, or other validation code. You could choose to handle that “LimitValue is a valid int” verification/conversion in several different ways.

    Of course, I haven’t implemented this in its entirety, but I wanted to outline a pattern where you are not using Commands to update the state of the other widgets. Instead, bind those attributes to properties that are managed in your ViewModel.

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

Sidebar

Related Questions

Having trouble with proper regex for RewriteCond RewriteCond %{REQUEST_URI} !^/foo/ Works as expected, that
I'm having trouble figuring out the best approach for an certain application. I'm not
I'm having trouble finding the GameKit api that contains the Game Center Reference. Apple
Having trouble with the binary_search function listed at the top. not sure where to
Having trouble here and not quite sure how to do it. I've attempted jQuery
Having trouble with this..I'm sure I'm missing something simple but I simply want the
am having trouble with reporting errors in zend framework, errors messages are not displayed
Having trouble linking the Stomp.framework into an iPhone SDK application. http://code.google.com/p/stompframework/ I follow the
Im having trouble using a .NET COM in vb6, It compiles ok and I
I having trouble in dividing the HTML frames. I have been using the following

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.