如何在C#中使用Caliburn.Micro从ListView中获取选定的项目和事件?

我用MVVM(C#)和XAML用Caliburn.Micro库编写了一个程序,我怎么能:

  • 获取所有选定的项目(不仅仅是一个项目)?
  • 获得选定的变更活动?
  • 单击标题列对项目进行排序?

任何帮助,将不胜感激。

GUI代码: Views \ MainView.xaml

        

ViewModel代码: ViewModels \ MainViewModel.cs

 using Caliburn.Micro; using ListBox_CaliburnMicro.Model; using ListBox_CaliburnMicro.ViewModels; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.ComponentModel; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Input; namespace ListBox_CaliburnMicro { public class MainViewModel : Screen, INotifyPropertyChanged { public MainViewModel() { FillDataFile(); HelloCommand = new RelayCommand(DoHello, CanDoHello); AddCommand = new RelayCommand(DoAdd, CanDoAdd); DeleteCommand = new RelayCommand(DoDelete, CanDoDelete); UpdateCommand = new RelayCommand(DoUpdate, CanDoUpdate); GetSelectedItemCommand = new RelayCommand(DoGetSelectedItem, CanDoGetSelectedItem); GetItemXCommand = new RelayCommand(DoGetGetItemX, CanDoGetItemX); } #region File listView private ObservableCollection _Files; public ObservableCollection Files { get { return _Files; } set { _Files = value; OnPropertyChanged("Files"); } } private void FillDataFile() { _Files = new ObservableCollection(); for (int i = 0; i = _Files.Count || nItem  0) return true; return false; } private void DoDelete(object obj) { if (_Files.Count > 0) { if (SelectedItem != null) _Files.Remove(SelectedItem); else _Files.RemoveAt(0); } } #endregion #region Update button public RelayCommand UpdateCommand { get; set; } private bool CanDoUpdate(object obj) { if (_Files.Count > 0) return true; return false; } private void DoUpdate(object obj) { // var vvv= SelectedItems; if (_Files.Count > 0) { if (SelectedItem != null) SelectedItem.FileName = "Updated ..."; // change FileName field else _Files[0].FileName = "Updated ..."; } // List FilesSelect = _Files.Where(p => p.FileID == _Files[0].FileID).ToList(); } #endregion #region GetSelectedItem button public RelayCommand GetSelectedItemCommand { get; set; } private bool CanDoGetSelectedItem(object obj) { if (_Files.Count > 0) return true; return false; } public File SelectedItem { get; set; } public int SelectedIndex { get; set; } private void DoGetSelectedItem(object obj) { if (_Files.Count > 0 && SelectedItem != null) { var selected_file = SelectedItem; OuputText = string.Format("first selected index {0} -> [{1}]", SelectedIndex, selected_file.FileID); } } #endregion #region GetItemX button public RelayCommand GetItemXCommand { get; set; } private bool CanDoGetItemX(object obj) { if (_Files.Count > 0) return true; return false; } private void DoGetGetItemX(object obj) { int nItem = 0; if (Int32.TryParse(ItemX, out nItem)) { if (nItem  [{1}]", nItem, _Files[nItem].FileID); } } else OuputText = "-"; } #endregion #endregion } } 

ICommand代码: ViewModels \ RelayCommand.cs

 using System; using System.Windows.Input; namespace ListBox_CaliburnMicro.ViewModels { public class RelayCommand : ICommand { #region field readonly Action _execute; readonly Predicate _canExecute; #endregion #region constructors public RelayCommand(Action execute) : this(execute, null) { } public RelayCommand(Action execute, Predicate canExecute) { if (execute == null) throw new ArgumentException("execute"); _execute = execute; _canExecute = canExecute; } #endregion #region member public bool CanExecute(object parameter) { return _canExecute == null ? true : _canExecute(parameter); } public event EventHandler CanExecuteChanged { add { CommandManager.RequerySuggested += value; } remove { CommandManager.RequerySuggested -= value; } } public void Execute(object parameter) { _execute(parameter); } #endregion #region raise public void RaiseCanExecuteChanged() { /*if (CanExecuteChanged != null) CanExecuteChanged(this, EventArgs.Empty);*/ } #endregion } } 

型号代码: Model \ File.cs

 using System; using System.ComponentModel; namespace ListBox_CaliburnMicro.Model { public class File : INotifyPropertyChanged { private Guid _FileID; public Guid FileID { get { return _FileID; } set { _FileID = value; OnPropertyChanged("FileID"); } } public event PropertyChangedEventHandler PropertyChanged; public void OnPropertyChanged(string propertyName) { if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } private string _FileStatus; public string FileStatus { get { return _FileStatus; } set { _FileStatus = value; OnPropertyChanged("FileStatus"); } } private string _FileName; public string FileName { get { return _FileName; } set { _FileName = value; OnPropertyChanged("FileName"); } } public int _FileSize; public int FileSize { get { return _FileSize; } set { _FileSize = value; OnPropertyChanged("FileSize"); } } private string _FileType; public string FileType { get { return _FileType; } set { _FileType = value; OnPropertyChanged("FileType"); } } private string _FileEmailCount; public string FileEmailCount { get { return _FileEmailCount; } set { _FileEmailCount = value; OnPropertyChanged("FileEmailCount"); } } private string _FileInfoCount; public string FileInfoCount { get { return _FileInfoCount; } set { _FileInfoCount = value; OnPropertyChanged("FileInfoCount"); } } public File() { FileID = Guid.NewGuid(); } public File(string s1 = "", string s2 = "", int s3 = 0, string s4 = "", string s5 = "", string s6 = "") { FileID = Guid.NewGuid(); FileStatus = s1; FileName = s2; FileSize = s3; FileType = s4; FileEmailCount = s5; FileInfoCount = s6; } } } 

您必须将这些事件连接到命令并在viewmodel中处理它们。

不使用Caliburn.Micro,但也应该有一个EventToCommand绑定的概念。 我快速搜索显示以下链接。

https://caliburnmicro.codeplex.com/wikipage?title=Cheat%20Sheet

https://caliburnmicro.codeplex.com/wikipage?title=All%20About%20Actions

http://www.mindscapehq.com/blog/index.php/2012/01/24/caliburn-micro-part-3-more-about-events-and-parameters/

如果我没有提出你的问题请告诉我…

HTH