如何使用DelegateCommand从另一个视图中删除绑定到DataGrid的ObservableCollection中的条目?

时间:2018-02-07 16:01:11

标签: c# wpf mvvm data-binding prism

现在我正在使用Prism Framework和Unity开发一个C#WPF应用程序。

我的申请表中有两个观点。第一个视图包含绑定到ObservableCollection的DataGrid。第二个视图是功能区中的RibbonTab,其中包含几个按钮。 我的目标是,如果按下RibbonTab上的删除按钮,则应删除DataGrid的选定元素。现在的问题是我也为每个视图都有视图模型。如何使用Prism Framework以MVVM方式解决我的问题?

我已经尝试使用在第一个viewmodel中编写的DelegateCommand,它使用Unity在第二个viewmodel中调用。该命令被调用,但DataGrid不会更新。我也使用RaisePropertyChanged();在我的ObservableCollection对象中......

SpottingsLogViewModel

namespace AirBase.Spottings.ViewModels
{
    public class SpottingsLogViewModel : ViewModelBase
    {
        #region Properties
        private ObservableCollection<Spotting> list;

    public ObservableCollection<Spotting> List
    {
        get { return list; }
        set
        {
            SetProperty(ref list, value);
            RaisePropertyChanged("list");
        }
    }

    private int selectedIndex;

    public int SelectedIndex
    {
        get { return selectedIndex; }
        set
        {
            SetProperty(ref selectedIndex, value);
            RaisePropertyChanged("selectedIndex");
        }
    }

    private string selectedItem;

    public string SelectedItem
    {
        get { return selectedItem; }
        set
        {
            SetProperty(ref selectedItem, value);
            RaisePropertyChanged("SelectedItem");
        }
    }
    #endregion

    #region Fields
    private IEventAggregator _eventAggregator;
    private ISpottingsLogService _spottingsLogService;
    #endregion

    #region Commands
    public DelegateCommand AddItemCommand { get; set; }
    public DelegateCommand DeleteSelectedItemCommand { get; set; }
    #endregion



    /// <summary>
    /// 
    /// </summary>
    public SpottingsLogViewModel()
    {
        //_spottingsLogService = spottingsLogService;

        AddItemCommand = new DelegateCommand(Add);
        DeleteSelectedItemCommand = new DelegateCommand(Delete);

        List = new ObservableCollection<Spotting>();

        LoadSpottings();


    }

    /// <summary>
    /// Gets all entries found in the database.
    /// </summary>
    private void LoadSpottings()
    {
        AirBaseDbContext context = new AirBaseDbContext();

        // Display all Blogs from the database 
        var query = from s in context.Spottings
                    select s;

        foreach (var item in query)
        {
            Spotting spotting = new Spotting()
            {
                Registration = item.Registration,
                Aircraft = item.Aircraft,
                Airline = item.Airline,
                Location = item.Location,
                Date = item.Date
            };
            List.Add(spotting);
        }
    }

    /// <summary>
    /// Only for testing purpose.
    /// </summary>
    private void Add()
    {
        Spotting spotting = new Spotting()
        {
            Registration = "LL",
            Aircraft = "737",
            Airline = "Lufthansa",
            Location = "EDDL",
            Date = new DateTime(2018, 07, 02)
        };
        List.Add(spotting);

        Console.WriteLine("Anzahl in der Liste: " + List.Count);
    }

    /// <summary>
    /// 
    /// </summary>
    private void Delete()
    {
        if(List.Count > 0)
            List.RemoveAt(SelectedIndex);
    }
}

}

SpottingsTabViewModel

namespace AirBase.Spottings.ViewModels
{
    public class SpottingsTabViewModel : BindableBase
    {
        #region Properties
        private string _title ="Logbook";
        public string Title
        {
            get { return _title; }
            set { _title = value; }
        }
        #endregion

    #region Fields
    private readonly IUnityContainer _container;
    private IEventAggregator _eventAggregator;
    #endregion

    #region Commands
    public DelegateCommand AddCommand { get; set; }
    public DelegateCommand DeleteCommand { get; set; }
    #endregion

    public SpottingsTabViewModel(IUnityContainer container)
    {
        _container = container;
        //_eventAggregator = eventAggregator;

        AddCommand = _container.Resolve<SpottingsLogViewModel>().AddItemCommand;
        DeleteCommand = _container.Resolve<SpottingsLogViewModel>().DeleteSelectedItemCommand;

    }
}

}

我必须要求视图模型和视图都在同一个模块中。

0 个答案:

没有答案