wpf - 一个与模型的多个实例交互的ViewModel

时间:2017-02-15 20:13:34

标签: c# wpf mvvm

我有一个WorkspaceViewModel,它通过ObservableCollection动态处理标签项的添加和删除。每次选项卡连接到PayslipModel时,所有绑定都可以正常工作,但我遇到的一个问题就是;

我在UserControl中有一个保存按钮,其DataContext设置为WorkspaceViewModel,我想保存所选标签中显示的所有信息。现在,每次添加选项卡时,都会创建一个新的PayslipModel实例,这正是我想要的,因为我不希望为所有选项卡共享绑定。但是,我无法保存显示的内容,因为PayslipModel有多个实例,因此当我点击保存时,没有返回任何内容(临时使用MessageBox来测试是否正在检索信息)。

我创建了一个图表来更好地解释我的情况: enter image description here

是否可以在选择选项卡时访问当前实例,或者在所有实例中循环并执行批量保存等操作?

1 个答案:

答案 0 :(得分:0)

这是一个工作示例,显示了其中一种可能性:

查看

    <TabControl DataContext="{Binding}" ItemsSource="{Binding Models}"  >
        <TabControl.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Name}" > 
                </TextBlock>
            </DataTemplate>
        </TabControl.ItemTemplate>
        <TabControl.ContentTemplate>
            <DataTemplate>
                <DockPanel>
                    <Button DockPanel.Dock="Top" Content="Click Me" Command="{Binding DataContext.PCommand, 
                                                            RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=TabControl}}"
                            CommandParameter="{Binding Desc}"/>
                    <TextBlock Text="{Binding Desc}" >
                    </TextBlock>
                </DockPanel> 
            </DataTemplate>
        </TabControl.ContentTemplate>
    </TabControl>

模型视图

public class ModelView
{
    public ModelView()
    {
        _models = new ObservableCollection<Model>(); 
        _pCommand = new Command(DoParameterisedCommand);
    }
    ObservableCollection<Model> _models;
    public ObservableCollection<Model> Models { get { return _models; } }

    private void DoParameterisedCommand(object parameter)
    {
        MessageBox.Show("Parameterised Command; Parameter is '" +
                     parameter.ToString() + "'.");
    }
    Command _pCommand;
    public Command PCommand
    {
        get { return _pCommand; }
    }
}

<强>模型

public class Model : INotifyPropertyChanged
{
    string _desc;
    public string Desc { get { return _desc; } set { _desc = value; RaisePropertyChanged("Desc"); } }

    string _name;
    public string Name { get { return _name; } set { _name = value; RaisePropertyChanged("Name"); } }

    public event PropertyChangedEventHandler PropertyChanged;
    void RaisePropertyChanged(string propname)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propname));
    }
}

<强>命令

public class Command : ICommand
{ 
    public Command(Action<object> parameterizedAction, bool canExecute = true)
    {
        _parameterizedAction = parameterizedAction;
        _canExecute = canExecute;
    }


    Action<object> _parameterizedAction = null;
    bool _canExecute = false;
    public bool CanExecute
    {
        get { return _canExecute; }
        set
        {
            if (_canExecute != value)
            {
                _canExecute = value;
                CanExecuteChanged?.Invoke(this, EventArgs.Empty);
            }
        }
    }
    public event EventHandler CanExecuteChanged;
    bool ICommand.CanExecute(object parameter)
    {
        return _canExecute;
    }

    void ICommand.Execute(object parameter)
    {
        this.DoExecute(parameter);
    }
    public virtual void DoExecute(object param)
    { if (_parameterizedAction != null)
            _parameterizedAction(param);
        else
            throw new Exception();
    }


}

使用它来初始化:

    public MainWindow()
    {
        InitializeComponent();
        ModelView mv = new ModelView();
        mv.Models.Add(new Model() { Name = "a", Desc = "aaa" });
        mv.Models.Add(new Model() { Name = "b" , Desc = "bbb"});
        mv.Models.Add(new Model() { Name = "c", Desc = "cccc" });
        this.DataContext = mv; 

    }