MVVM Design Issues

I am new to WPF and MVVM and struggle a lot with how to develop my application (hence there are a lot of questions here), and I'm going to do refactoring. I have a SQL server and I use LINQtoSQL, so the classes that LINQTOSQL generates will be my model. I have yet to see a good comprehensive example of how to remove model classes from a view — in the example I'm looking at now, the view is a binding to ModelClass.Something instead of the ViewModel property that references ModelClass.Something. Do I correctly believe that this is bad (if this field changes in db view breaks)?

I am currently struggling with dropdowns. Example. I have a lookup table named MessageType with id and text box and a repository method to retrieve them. Therefore, in my opinion, I want to avoid binding the DisplayMemberPath and SelectedValue paths to the fields in the Model MessageType class, so I need to create a new class and show the class properties in my ViewModel? Like this:

public class MessageTypeViewModel : ViewModelBase
{
    public MessageTypeViewModel(MessageType t)
    {
        MessageTypeText = t.messageTypeText;
        MessageTypeId = t.messageTypeId;
    }
    public string MessageTypeText { get; private set; }//bind DisplayMember and SelectedValue to these properties
    public int MessageTypeId { get; private set; }
}

filled as follows:

get
{
if (textMessageSelectionOptions == null)
    {
        var list = repository.GetMessageTypes().Select(x=>new MessageTypeViewModel(x)).ToList();
        textMessageSelectionOptions =new ReadOnlyCollection<MessageTypeViewModel>(list);
    }
return textMessageSelectionOptions;
}

I also have a Mission object that has a foreign key for MessageType, since I need to bind the combo box SelectedItem field to the mission. MessageType, given that I now have this new class? I am really trying to get a template with the mission object, its children and its parents, and I cannot find comprehensive examples when people do CRUD operations with MVVM. Am I getting to the top or is something missing?

+3
3

MVVM , . MVVM , . , - ViewModel , , MVVM .

, , , ViewModel (, Mission → MessageType), .

: http://msdn.microsoft.com/en-us/magazine/ff798279.aspx, .

+3

, MessageType , , .

, , - MissionViewModel, , MessageType, , getter, , .

MessageType ( ItemsSource) ToString() . , , , IValueConverter , .

MessageType, mission.MessageType SelectedItem IValueConverter, MessageType, ,

. MessageType ,
 B. IValueConverter, MessageType,  C. IValueConverter , , MessageType

B , XAML ResourceDictionary .

+1

You can use the command instead of directly accessing the view from the viewmodel or vice versa. See WPF Applications with Model-View-ViewModel Design Pattern

0
source

All Articles