【发布时间】:2015-05-21 23:56:57
【问题描述】:
假设我们有一个具有以下属性的 Model(Model 类)。
public string InputFileName
{
get { return m_InputFileName; }
set
{
m_InputFileName = value;
RaiseNotifyPropertyChanged("InputFileName");
}
}
上面的模型实现了INotifyPropertyChanged接口,所以我们也有下面的方法和下面的事件。下面的RaiseNotifyPropertyChanged 方法用于更新ViewModel。
#region INotifyPropertyChanged Implementation
private void RaiseNotifyPropertyChanged(string property)
{
var handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(property));
}
}
public event PropertyChangedEventHandler PropertyChanged;
#endregion
以下是实现ViewModel的类的主要部分。
public class ViewModel : INotifyPropertyChanged
{
#region Members
private Model m_Model;
private string m_InputFileStr;
private readonly ICommand m_SubmitCommand;
#endregion
#region Constructors
public ViewModel()
{
m_Model = new Model();
m_Model.PropertyChanged += new PropertyChangedEventHandler(this.Model_PropertyChanged);
m_InputFileStr = string.Empty;
// ...
// initialize m_SubmitCommand
}
#endregion
// ...
#region Properties
public string InputFileStr
{
get { return m_InputFileStr; }
set
{
if (value == m_InputFileStr) return;
m_InputFileStr = value;
OnPropertyChanged("InputFileStr");
m_SubmitCommand.RaiseCanExecuteChanged();
}
}
#endregion
#region INotifyPropertyChanged Implementation
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
#endregion
// This method is called when the model changes, so the Model notified the ViewModel.
private void Model_PropertyChanged(object sender, PropertyChangedEventArgs args)
{
if (args.PropertyName == "InputFileName")
{
InputFileStr = m_Model.InputFileName;
}
else if (args.PropertyName == "OutputFileName")
{
OutputFileStr = m_Model.OutputFileName;
}
else if (args.PropertyName == "ReportText")
{
ReportTextStr = m_Model.ReportText;
}
}
}
以下是实现View的类的主要部分:
MainWindow.xaml
<TextBox Name="inputfileTextBox"
Text="{Binding Path=InputFileStr, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"/>
<Button Name="submitButton"
Content="Submit"
Command="{Binding SubmitCommand}"/>
MainWindow.xaml.cs
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.DataContext = new ViewModel();
}
}
上述实现工作正常:
- View 和 ViewModel 正确地相互更新;
- 模型正确更新了 ViewModel。
为了使 ViewModel 能够更新模型,我想我会在 ViewModel 的 set 属性 InputFileStr 中添加以下调用:
m_Model.InputFileName = value;
但是,这种更新模型的解决方案会导致明显的意外效果:
- 用户修改了视图。
- ViewModel 会自动修改。
- ViewModel 更新模型 (
m_Model.InputFileName = value;)。 - 模型已更新...
- ...所以它会通知 ViewModel 有关更改
上述行为是否正确?我希望如果 ViewModel 更新了 Model,那么 Model 不必重新通知 ViewModel 相同的更改...作为替代解决方案,我想我会在 Model 中添加一个 Update 方法:此方法应该在不使用模型属性的情况下更新模型。
public void Update(string inputFileName) // this method does not notifies the ViewModel
{
m_InputFileName = inputFileName;
}
这种替代解决方案是正确的解决方案还是有更好的解决方案?
【问题讨论】:
标签: c# .net wpf user-interface mvvm