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
forLimitChk
(enable and disable) or just one (toggle)? - If I bind an
int
toLimitTxt
, what happens if I make it empty and disable it? - Is it a clean way to just use
DoStuff(Int32.Parse(LimitTxt.Text))
whenDoStuffBtn
is pressed? - If I use two commands on
LimitChk
, what happens with theCanExecute()
function ofICommand
that determines whetherLimitChk
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.