Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

So given the Separation of Concerns, how coupled should the View and ViewModel be?

For example, I want the visibility of a Control in the View to be databound (databinded?) to a flag in the ViewModel.

My first hunch would be to use a boolean value, IsControlVisible that returns true/false. However, in the View, visibility is set by an Enum.

So I have a choice: change the property to an Enum, or use a Converter to convert the bool into the Visibility Enum.

Which is the proper approach when trying to follow idiomatic MVVM?

share|improve this question

migrated from stackoverflow.com Jun 16 '11 at 12:44

This question came from our site for professional and enthusiast programmers.

    
Data bound is correct, not data binded ;-) –  CodeNaked Jun 15 '11 at 17:48
    
2 votes for off-topic? –  Alan Jun 16 '11 at 3:31
add comment

3 Answers

up vote 7 down vote accepted

If the View-Model is only concerned with a control being visible or collapsed, then I'd make it a boolean and use a value converter. If the View-Model also needs to communicate the hidden state, then I'd make it an enum (and probably use the Visibility enum).

So to answer your question, I'd have the View-Model express what it needs (visible/collapsed or visible/collapsed/hidden) and adapt the View to fit with the value converter. Other cases may vary though, as with most things.

share|improve this answer
1  
Why does he need a converter? That's just creating more work for no benefit. –  O.O Jun 15 '11 at 17:52
2  
@subt13 - There is actually a built in converter for just this case (BooleanToVisibilityConverter). It makes the View-Model much clearer, especially if the Hidden state is not valid. By using an Enum, your VM can be invalid. –  CodeNaked Jun 15 '11 at 17:55
1  
@subt13 - I disagree. Having a property that matches type is not the cleanest way. Let's say you have an IsError property and you want to display something in Bold when that is true. Under your model, you'd have to have an ErrorFontWeight property. Whereas the best way to do that would be through a value converter or trigger. –  CodeNaked Jun 15 '11 at 18:03
2  
@subt13 - Possibly, but in general the VM shouldn't be adapted to fit the View, it should be the other way around. –  CodeNaked Jun 15 '11 at 18:19
3  
+1 A converter would be what I would recommend too. It allows you to keep your view and viewModel separate and makes it easier to replace the front-end if needed (say a web-app in asp.net using the existing viewmodels). You might also want to do a DataTrigger based on the value, in which case its much better to keep it as a boolean. –  Rachel Jun 15 '11 at 18:21
show 10 more comments

The question you need to ask is "why is this widget being hidden?"

If the answer is because it's not available, your VM property should probably be a bool called IsAvailable. If it's because some other property has or doesn't have a particular value, do it with an IValueConverter.

The general concept is that the VM holds the conceptual state of the system, and the View decides how to visually represent that.

share|improve this answer
add comment

Your ViewModel could expose property of type Visibility instead of a bool and bind directly to that.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.