如何在C#中使用Caliburn.Micro更新ListView中的项目?

时间:2016-02-03 10:22:24

标签: c# listview caliburn.micro

我使用Caliburn.Micro库编写了一个使用MVVM(C#)和XAML的简单程序, 但有些行动并不起作用:

  • 更新项目内容
  • 获取所选项目
  • 获取特定项目
  • 向上移动并向下移动记录
  • 单击“添加”按钮时启用或禁用按钮

任何帮助都将不胜感激。

我的GUI代码:

// Views\MainView.xaml
<Window x:Class="ListBox_CaliburnMicro.MainView"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:cal="clr-namespace:Caliburn.Micro;assembly=Caliburn.Micro"
        xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
        xmlns:vm="clr-namespace:ListBox_CaliburnMicro"
        WindowStartupLocation="CenterScreen"
        Title="MainWindow" Height="300" Width="600">
    <Grid>
        <Grid.DataContext>
            <vm:MainViewModel/>
        </Grid.DataContext>

        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="153*"/>
        </Grid.ColumnDefinitions>

        <Button Content="Add" Command="{Binding AddCommand}" Grid.Column="1" HorizontalAlignment="Left" Margin="10,37,0,0" VerticalAlignment="Top" Width="75" ToolTip="Add item end of list" Background="White" Foreground="Black" Height="22"/>
        <Button Content="Delete" Command="{Binding DeleteCommand}" Grid.Column="1" HorizontalAlignment="Left" Margin="96,37,0,0" VerticalAlignment="Top" Width="75" ToolTip="Delete first item from list" Background="White" Foreground="Black" Height="22"/>
        <Button Content="Update" Command="{Binding UpdateCommand}" Grid.Column="1" HorizontalAlignment="Left" Margin="184,37,0,0" VerticalAlignment="Top" Width="75" ToolTip="Update first item from list" Background="White" Foreground="Black" Height="22"/>
        <Button Content="GetSelectedItem" Command="{Binding GetSelectedItemCommand}" Grid.Column="1" HorizontalAlignment="Left" Margin="271,37,0,0" VerticalAlignment="Top" Width="95" ToolTip="get selected item from list" Background="White" Foreground="Black" Height="22"/>
        <Button Content="GetItem:" Command="{Binding GetItemXCommand}" Grid.Column="1" HorizontalAlignment="Left" Margin="377,37,0,0" VerticalAlignment="Top" Width="75" ToolTip="get item x, from list" Background="White" Foreground="Black" Height="22"/>

        <TextBox Grid.Column="1" x:Name="ItemX" Text="0" HorizontalAlignment="Left" Height="24" Margin="457,35,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="70"/>

        <ListView Grid.Column="1" x:Name="FileListView" ItemsSource="{Binding Path=Files}" VerticalAlignment="Stretch" HorizontalContentAlignment="Stretch" Margin="10,65,10,10" Foreground="Black" Background="#FFE6EEF7">
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="Status" Width="Auto"
                    DisplayMemberBinding="{Binding FileStatus}"/>
                    <GridViewColumn Header="Name" Width="Auto"
                    DisplayMemberBinding="{Binding FileName}"/>
                    <GridViewColumn Header="Size" Width="Auto"
                     DisplayMemberBinding="{Binding FileSize}"/>
                    <GridViewColumn Header="System Type" Width="Auto"
                    DisplayMemberBinding="{Binding FileType}"/>
                    <GridViewColumn Header="Email Count" Width="Auto"
                     DisplayMemberBinding="{Binding FileEmailCount}"/>
                    <GridViewColumn Header="Info Count" Width="Auto"
                     DisplayMemberBinding="{Binding FileInfoCount}"/>
                </GridView>
            </ListView.View>
        </ListView>

    </Grid>
</Window>

我的模特是:

// Model\File.cs
using System;

namespace ListBox_CaliburnMicro.Model
{
    public class File
    {
        private Guid _FileID;
        public Guid FileID
        {
            get
            {
                return _FileID;
            }
            set 
            {
                _FileID = value;
            }
        }
        public string FileStatus { get; set; }
        public string FileName { get; set; }
        public string FileSize { get; set; }
        public string FileType { get; set; }
        public string FileEmailCount { get; set; }
        public string FileInfoCount { get; set; }

        public File()
        {
            FileID = Guid.NewGuid();
        }
        public File(string s1 = "", string s2 = "", string s3 = "", string s4 = "", string s5 = "", string s6 = "")
        {
            FileID = Guid.NewGuid();

            FileStatus = s1;
            FileName = s2;
            FileSize = s3;
            FileType = s4;
            FileEmailCount = s5;
            FileInfoCount = s6;
        }
    }
}

我的命令处理程序代码:

// ViewModels\CommandHandler.cs
using System;
using System.Windows.Input;

namespace ListBox_CaliburnMicro.ViewModels
{
    public class CommandHandler : ICommand
    {
        private System.Action _action;
        private bool _canExecute;
        public CommandHandler(System.Action action, bool canExecute)
        {
            _action = action;
            _canExecute = canExecute;
        }
        public bool CanExecute(object parameter)
        {
            return _canExecute;
        }

        public event EventHandler CanExecuteChanged;
        public void Execute(object parameter)
        {
            _action();
        }
    }
}

我的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();

            _canAddCommandExecute = true;
            _canDeleteCommandExecute = false;
            _canUpdateCommandExecute = false;
            _canGetSelectedItemCommandExecute = false;
            _canGetItemXCommandExecute = false;
        }

        #region File listView
        private ObservableCollection<File> _Files;
        public ObservableCollection<File> Files
        {
            get { return _Files; }
            set 
            {
                _Files = value;
                OnPropertyChanged("Files");
            }
        }

        private void FillDataFile()
        {
            _Files = new ObservableCollection<File>();
            for (int i = 0; i < 0; i++)
            {
                _Files.Add(new File() { FileStatus = "status", FileName = "name", FileSize = "size", FileType = "type", FileEmailCount = "email_count", FileInfoCount = "info_count" });
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        public void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
        #endregion

        #region ItemX textbox
        private string _ItemX = "0";
        public string ItemX
        {
            get 
            {
                return _ItemX;
            }
            set 
            {
                _ItemX = value;
            }
        }
        #endregion

        #region command button

        #region Add button with Command
        private ICommand _AddCommand;
        public ICommand AddCommand
        {
            get
            {
                return _AddCommand ?? (_AddCommand = new CommandHandler(() => AddAction(), _canAddCommandExecute));
            }
        }

        static int nIndex = 0;

        private bool _canAddCommandExecute;
        public void AddAction()
        {
            string strName = string.Format("{0}", nIndex++);
            _Files.Add(new File() { FileStatus = "status", FileName = strName, FileSize = "size", FileType = "type", FileEmailCount = "email_count", FileInfoCount = "info_count" });

            // enabled action buttons,
            // !!! but not worded !!!
            _canAddCommandExecute = true;
            _canDeleteCommandExecute = true;
            _canUpdateCommandExecute = true;
            _canGetSelectedItemCommandExecute = true;
            _canGetItemXCommandExecute = true;


        }
        #endregion

        #region Delete button with Command
        private ICommand _DeleteCommand;
        public ICommand DeleteCommand
        {
            get
            {
                return _DeleteCommand ?? (_DeleteCommand = new CommandHandler(() => DeleteAction(), _canDeleteCommandExecute));
            }
        }

        private bool _canDeleteCommandExecute;
        public void DeleteAction()
        {
            if (_Files.Count > 0)
                _Files.RemoveAt(0);
        }
        #endregion

        #region Update button with Command
        private ICommand _UpdateCommand;
        public ICommand UpdateCommand
        {
            get
            {
                return _UpdateCommand ?? (_UpdateCommand = new CommandHandler(() => UpdateAction(), _canUpdateCommandExecute));
            }
        }

        private bool _canUpdateCommandExecute;
        public void UpdateAction()
        {
            if (_Files.Count > 0)
            {
                // update FileName field
                _Files[0].FileName = "+"; // !!! but didn't work !!!

                // List<File> FilesSelect = _Files.Where(p => p.FileID == _Files[0].FileID).ToList();
            }
        }
        #endregion

        #region GetSelectedItem button with Command
        private ICommand _GetSelectedItemCommand;
        public ICommand GetSelectedItemCommand
        {
            get
            {
                return _GetSelectedItemCommand ?? (_GetSelectedItemCommand = new CommandHandler(() => GetSelectedItemAction(), _canGetSelectedItemCommandExecute));
            }
        }

        private bool _canGetSelectedItemCommandExecute;
        public void GetSelectedItemAction()
        {
            if (_Files.Count > 0)
            {
                // ... get selected item in ListView
            }
        }
        #endregion

        #region GetItemX button with Command
        private ICommand _GetItemXCommand;
        public ICommand GetItemXCommand
        {
            get
            {
                return _GetItemXCommand ?? (_GetItemXCommand = new CommandHandler(() => GetItemXAction(), _canGetItemXCommandExecute));
            }
        }

        private bool _canGetItemXCommandExecute;
        public void GetItemXAction()
        {
            if (_Files.Count > 0)
            {
                // ... get item 'X' that enter in TextBox control
            }
        }
        #endregion

        #endregion
    }
}

1 个答案:

答案 0 :(得分:2)

更新商品内容。  要在视图中查看更新的模型项,您应该在模型的 EACH 属性中实现INotifyPropertyChanged事件。例如:

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));
        }
    }        
}

获取所选项目。 在ViewModel中创建一个属性以保存所选项目,例如File

public File SelectedFile { get; set; }

然后将SelectedItem的{​​{1}}绑定到此属性:

ListView

在您实施<ListView Name="UserGrid" SelectedItem="{Binding SelectedFile}" ItemsSource="{Binding Path=Files}"/> 属性后,您可以采用以下值:

SelectedFile

单击“添加”按钮时启用或禁用按钮。 您有两种方式:Caliburn和Caliburn

使用CaliburnMicro: CaliburnMicro有自己的机制来处理Button if(SelectedFile!=null) var exactFile=SelectedFile; 事件。例如:

Click

要查看有关实施的更多详细信息,请参阅official guide at CodePlex

没有CaliburnMicro:

首先,你应该创建<Button Content="ShowPageTwo"> <i:Interaction.Triggers> <i:EventTrigger EventName="Click"> <cal:ActionMessage MethodName="ShowPageTwo" /> </i:EventTrigger> </i:Interaction.Triggers> </Button> 类,通过调用委托来将其功能转发给其他对象:

RelayCommand

然后在viewModel中创建一个属性。例如:

public class RelayCommand : ICommand
{
    #region Fields

    readonly Action<object> _execute;
    readonly Predicate<object> _canExecute;

    #endregion // Fields

    #region Constructors

    public RelayCommand(Action<object> execute)
        : this(execute, null)
    {
    }

    public RelayCommand(Action<object> execute, Predicate<object> canExecute)
    {
        if (execute == null)
            throw new ArgumentNullException("execute");

        _execute = execute;
        _canExecute = canExecute;
    }
    #endregion // Constructors

    #region ICommand Members

    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 // ICommand Members
}

XAML应该看起来像:

public class YourViewModel
{
    public RelayCommand YourCommand { get; set; }
    public YourViewModel()
    {
        YourCommand = new RelayCommand(DoSmth, CanDoSmth);
    }

    private void DoSmth(object obj)
    {
        Message.Box("Hello from viewModel"); 
    }

    private bool CanDoSmth(object obj)
    {
       //you could implement your logic here. But by default it should be  
       //set to true
       return true;
    }
}