0

我有 3 张桌子

用户:

  id
  name

偏爱:

  id:
  title:

用户偏好:

  user_id
  preference_id

如何为保存数据创建视图模型并创建这样的表单?

New User

Name
|------------|
|textbox     | 
|------------|

Your Preferences
[ ] Preference1
[ ] Preference2
[ ] Preference3
..
[ ] PreferenceN
4

1 回答 1

0

我相信,它应该看起来像这样:

使用 C# 的元代码

// You can realize INotificationChanged here too if you need
sealed class Preference 
{
  public Preference(string name, bool isSelected)
  {
    Name = name; 
    IsSelected = isSelected;
  }

  public string Name { get; private set; }
  public bool IsSelected { get; set; }  
}

sealed class User : INotificationChanged
{
  private ObservaleCollection<Preference> viewPreferences;

  public event PropertyChangedEventHandler PropertyChanged;

  public User(PreferenceCollection preferences)
  {
    PreferenceTitle = preferences.Title;
    // put info to collection
  }

  public ObservaleCollection<Preference> Preferences
  {
    get { return preferences; }
  }

  // You can remove set if you don't want to set it directly
  public UserId { get; set; }
  public UserName { get; set; }
  public PreferenceTitle { get; private set; }

  // Use it to update data in view
  protected void OnPropertyChanged(string name)
  {
      PropertyChangedEventHandler handler = PropertyChanged;
      if (handler != null)
      {
          handler(this, new PropertyChangedEventArgs(name));
      }
  }
}

它只是一个模型的模型。当然,您需要添加验证,保存数据逻辑并将属性绑定到 xaml 表单

于 2012-07-09T04:29:57.973 回答