Click here to Skip to main content
15,898,939 members
Home / Discussions / WPF
   

WPF

 
AnswerRe: A few newb MVVM (WPF) questions... Pin
Mycroft Holmes17-Oct-10 23:40
professionalMycroft Holmes17-Oct-10 23:40 
AnswerRe: A few newb MVVM (WPF) questions... Pin
Pete O'Hanlon18-Oct-10 0:32
mvePete O'Hanlon18-Oct-10 0:32 
GeneralRe: A few newb MVVM (WPF) questions... Pin
SledgeHammer0118-Oct-10 7:57
SledgeHammer0118-Oct-10 7:57 
GeneralRe: A few newb MVVM (WPF) questions... Pin
Pete O'Hanlon18-Oct-10 9:55
mvePete O'Hanlon18-Oct-10 9:55 
GeneralRe: A few newb MVVM (WPF) questions... Pin
SledgeHammer0118-Oct-10 10:37
SledgeHammer0118-Oct-10 10:37 
GeneralRe: A few newb MVVM (WPF) questions... Pin
Pete O'Hanlon19-Oct-10 2:18
mvePete O'Hanlon19-Oct-10 2:18 
AnswerRe: A few newb MVVM (WPF) questions... Pin
si61821-Oct-10 19:33
si61821-Oct-10 19:33 
AnswerRe: A few newb MVVM (WPF) questions... [SOLUTION] Pin
AndrewSmith22-Oct-10 17:08
AndrewSmith22-Oct-10 17:08 
I too often wondered about things like this. But I have finally come to terms that I think makes the most sense.

1) Go ahead and use the CurrentItem and force that behavior on the ViewModel. I think the hard fact is that ViewModels shouldn't be used across different presentation frameworks (e.g. WebForms, Asp.Net MVC, Windows Forms, WPF). This shouldn't happen for a multitude of reasons. The major point is your going to paint yourself into a very difficult and hard to use ViewModel to try and achieve compatibility with all of these frameworks. I have often fought with this too, and I came to some really good conclusions trying to learn the real way to treat ViewModels.

* ViewModels work best as an organizational structure. I treat ViewModels as an organization unit and not another technical layer of abstractions. This will give you optimal productivity and fewer bugs. With a well structured and organized ViewModel you create a more maintainable and agile product base. You shouldn't think about using ViewModels in "other" places than the one your creating it for, because it just doesn't work well.

* ViewModel code should be very simple. This means that the ViewModel code should be very simple that even a 2 year old can write them. Sure you can have a ViewModelBase class that contains some important infrastructure for your application, but after that your ViewModel code should be simple. With really simple viewmodel code, you can write up a ViewModel for exactly what you need in a few minutes.


Now, i know what some people are going to complain saying that they want to have a single ViewModel for everything. But with a focused and really simple to write ViewModels. It's painless to create more classes for your program to do exactly what you need.

Does this mean that your going to have a lot of ViewModels? YES.
Does this mean you have to write a ViewModel for Windows Forms and a different one for WPF? YES

If you think ViewModels should be another component of re-use, please take a look at this post from Udi about the fallacy of re-use.
http://www.udidahan.com/2009/06/07/the-fallacy-of-reuse/[^]




2) For the Commands, I like to use the RelayCommand implementation: http://msdn.microsoft.com/en-us/magazine/dd419663.aspx#id0090030[^]

This is pretty much "The" way to handle code only ViewModels. This allows to have the behavior that your looking for.

<br />
public Person SelectedPerson { get; set; } // <-- this should be a dependency property or notify property to support two-way binding<br />
<br />
public ICommand EditCommand { get; private set; }<br />
<br />
private void OnEdit(object arg)<br />
{<br />
    //Edit the SelectedPerson<br />
}<br />
<br />
public SomeViewModel()<br />
{<br />
    EditCommand = new RelayCommand(OnEdit, x => SelectedPerson != null);<br />
}<br />



This works really well, and it's very simple too!
QuestionConnect silverlight project to web project. Pin
dbrenth15-Oct-10 10:55
dbrenth15-Oct-10 10:55 
AnswerRe: Connect silverlight project to web project. Pin
Abhinav S15-Oct-10 18:41
Abhinav S15-Oct-10 18:41 
QuestionDynamically instantiate UserControl, try to find which UserControl fired an event in an event Handler? Pin
Jean DUSUD14-Oct-10 11:38
Jean DUSUD14-Oct-10 11:38 
AnswerRe: Dynamically instantiate UserControl, try to find which UserControl fired an event in an event Handler? Pin
Abhinav S14-Oct-10 17:24
Abhinav S14-Oct-10 17:24 
AnswerRe: Dynamically instantiate UserControl, try to find which UserControl fired an event in an event Handler? Pin
#realJSOP15-Oct-10 9:04
professional#realJSOP15-Oct-10 9:04 
GeneralRe: Dynamically instantiate UserControl, try to find which UserControl fired an event in an event Handler? Pin
Jean DUSUD18-Oct-10 11:00
Jean DUSUD18-Oct-10 11:00 
QuestionDataGrid SelectionChanged event is firing only on first row selected. Pin
Member 392066714-Oct-10 4:56
Member 392066714-Oct-10 4:56 
AnswerRe: DataGrid SelectionChanged event is firing only on first row selected. Pin
torken214-Oct-10 20:37
professionaltorken214-Oct-10 20:37 
QuestionReportViewer does not export PDF for wingdings fonts Pin
ausadmin12-Oct-10 11:15
ausadmin12-Oct-10 11:15 
AnswerRe: ReportViewer does not export PDF for wingdings fonts Pin
phil.o13-Oct-10 2:24
professionalphil.o13-Oct-10 2:24 
GeneralRe: ReportViewer does not export PDF for wingdings fonts Pin
ausadmin17-Oct-10 10:51
ausadmin17-Oct-10 10:51 
GeneralRe: ReportViewer does not export PDF for wingdings fonts Pin
ausadmin31-Oct-10 12:46
ausadmin31-Oct-10 12:46 
AnswerRe: Prism- Can't add reference to Unity Pin
Abhinav S12-Oct-10 4:59
Abhinav S12-Oct-10 4:59 
GeneralRe: Prism- Can't add reference to Unity Pin
Abhinav S12-Oct-10 5:51
Abhinav S12-Oct-10 5:51 
GeneralRe: Prism- Can't add reference to Unity Pin
Abhinav S12-Oct-10 6:45
Abhinav S12-Oct-10 6:45 
QuestionFail to do binding Template Property Pin
Yanshof12-Oct-10 3:42
Yanshof12-Oct-10 3:42 
AnswerRe: Fail to do binding Template Property Pin
Ravi Mori14-Oct-10 21:29
Ravi Mori14-Oct-10 21:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.