MVVM模型 - 模型之间的关联

时间:2011-07-18 14:26:57

标签: c# wpf mvvm

假设我有以下模型:

class Worker
{
    int Id;
    string firstname;
    string lastname;    
}

class Department
{
    string title;
    string description; 
    List<Worker> workers;
}

我想在UI上显示部门的标题,描述和列表框内的工作人员列表(在列表框中我只想显示名字和姓氏)。
我是否需要创建一个包含此关系的viewmodel,或者我必须为每个模型创建一个viewmodel?

3 个答案:

答案 0 :(得分:3)

您可以在ViewModel上创建包含它们的内容,如:

namespace XXXX.ViewModel
{
public class MainViewModel : ViewModelBase
{
    private int _id;
    private string _total;
    private string _description;

    private ObservableCollection<Worker> _workers;

    public int Id
    {
        get { return _id; }
        set
        {
            if (value == _id) return;
            _id = value;
            RaisePropertyChanged("Id");
        }
    }
    public string Total
    {
        get { return _total; }
        set
        {
            if (value == _total) return;
            _total = value;
            RaisePropertyChanged("Total");
        }
    }
    public string Description
    {
        get { return _description; }
        set
        {
            if (value == _description) return;
            _description = value;
            RaisePropertyChanged("Description");
        }
    }
    public ObservableCollection<Worker> Workers
    {
        get { return _workers; }
        set
        {
            if (value == _workers) return;
            _workers = value;
            RaisePropertyChanged("Workers");
        }
    }

    //****************** You Logic *************************
    public MainViewModel()
    {
        Department department = new Department();
    }
    //****************** You Logic *************************
}
}

答案 1 :(得分:2)

对于每个Model都不会有ViewModel,在MVVM中,几乎每个视图都应该有一个唯一的ViewModel。然后,您可以将模型映射到ViewModel。

例如:

 public class DepartmentViewModel
        {
            public string title { get; set; }

            public string description { get; set; }

            public IEnumerable<Worker> workers { get; set; }

            //Additional ViewModel properties here
            //These may or may not be items that exist in your Model

            /// <summary>
            /// Mapped to the description but truncated to 10 characters and followed by an elispe (...)
            /// </summary>
            public string ShortDescription
            {
                get
                {
                    return description.Substring(0,10) + "...";
                }
            }
    }

我一开始意识到这看起来有点多余。但是,您可以从模型中创建其他较少的1:1类型的视图。

另请查看automapper.org,这是将对象映射到对象的绝佳工具。

答案 2 :(得分:0)

您有一个包含工人和部门的视图模型。

如果视图只想显示worker的某些属性,那么视图应该进行过滤。尝试使用项目模板:

<ListBox x:Name="_workers" ItemsSource="{Binding Workers}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding firstname}" />
            <TextBlock Text=" " />
            <TextBlock Text="{Binding lastname}" />
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

视图模型应包含:

private string _title;
public string Title {
    get {return _title;}
    set {_title = value; RaisePropertyChanged("Title");}
}
private string _description;
public string Description {
    get {return _description;}
    set {_description= value; RaisePropertyChanged("Description");}
}
public ObservableCollection Workers {get; private set;}

public Constructor()
{
    Workers = new ObservableCollection();
}

//This method is called by the model once it has fetched data.
//This can be done as a callback or in an event handler
public CalledByTheModelAfterLoadingData(Department department)
{
    Title = department.Title;
    Description = department.Description;
    foreach (var worker in department.Workers)
    {
        Workers.Add(worker);
    }
}