我怎样才能在C#中选择项目(S)和事件的ListView与Caliburn.Micro? [英] How can i get selected item(s) and events from ListView with Caliburn.Micro in C#?

查看:243
本文介绍了我怎样才能在C#中选择项目(S)和事件的ListView与Caliburn.Micro?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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


  • 获得全部所选的项目(不仅是一个项目)?

  • 选择得到改变事件?

  • 排序点击标题列的项目?

任何帮助将是AP preciated。

GUI code:视图\\ MainView.xaml

 <窗​​口x:类=ListBox_CaliburnMicro.MainView
        的xmlns =htt​​p://schemas.microsoft.com/winfx/2006/xaml/$p$psentation
        的xmlns:X =htt​​p://schemas.microsoft.com/winfx/2006/xaml
        的xmlns:CAL =CLR的命名空间:Caliburn.Micro;装配= Caliburn.Micro
        的xmlns:I =CLR的命名空间:System.Windows.Interactivity;装配= System.Windows.Interactivity
        的xmlns:VM =CLR的命名空间:ListBox_CaliburnMicro
        WindowStartupLocation =中心屏幕
        标题=主窗口HEIGHT =300WIDTH =600>
    <网格和GT;
        < Grid.DataContext>
            < VM:MainViewModel />
        < /Grid.DataContext>        < Grid.ColumnDefinitions>
            < ColumnDefinition WIDTH =153 */>
        < /Grid.ColumnDefinitions>        <按钮命令={结合HelloCommand}CONTENT =你好......的Horizo​​ntalAlignment =左保证金=10,36,0,0VerticalAlignment =评出的WIDTH =75工具提示=测试背景=白前景=黑HEIGHT =22/>
        <按钮内容=add命令={结合AddCommand}的Horizo​​ntalAlignment =左保证金=10,8,0,0VerticalAlignment =评出的WIDTH =75工具提示=添加列表项的结束背景=白前景=黑HEIGHT =22/>
        <按钮内容=删除命令={结合的DeleteCommand}的Horizo​​ntalAlignment =左保证金=96,8,0,0VerticalAlignment =评出的WIDTH =75工具提示=删除从列表中的第一个项目背景=白前景=黑HEIGHT =22/>
        <按钮内容=更新命令={结合的UpdateCommand}的Horizo​​ntalAlignment =左保证金=184,8,0,0VerticalAlignment =评出的WIDTH =75工具提示=更新列表,从第一个项目背景=白前景=黑HEIGHT =22/>
        <按钮内容=GetSelectedItem命令={结合GetSelectedItemCommand}的Horizo​​ntalAlignment =左保证金=271,8,0,0VerticalAlignment =评出的WIDTH =95工具提示=获得选择从列表项背景=白前景=黑HEIGHT =22/>
        <按钮内容=的GetItem:命令={结合GetItemXCommand}的Horizo​​ntalAlignment =左保证金=377,8,0,0VerticalAlignment =评出的WIDTH =75工具提示=拿到项目X,从名单背景=白前景=黑HEIGHT =22/>        <文本框X:NAME =ItemX文本=0的Horizo​​ntalAlignment =左身高=20保证金=455,10,0,0TextWrapping =包装VerticalAlignment =评出的WIDTH =70 />        < TextBlock的X:名称=OuputText的Horizo​​ntalAlignment =左身高=20保证金=165,40,0,0TextWrapping =自动换行文本=VerticalAlignment =评出的WIDTH =415前景=#FFFF0909/>
        <标签内容=输出的Horizo​​ntalAlignment =左身高=28保证金=100,35,0,0VerticalAlignment =评出的WIDTH =55/>        < ListView控件Grid.Column =1×:NAME =FileListView的SelectionMode =扩展的SelectedItem ={结合的SelectedItem}的SelectedIndex ={结合的SelectedIndex}的ItemsSource ={绑定路径=文件}VerticalAlignment = 拉伸Horizo​​ntalContentAlignment =拉伸保证金=10,65,10,10前景=黑背景=#FFE6EEF7>            < ListView.View>
                <&GridView的GT;
                    < GridViewColumn标题=状态WIDTH =自动
                    DisplayMemberBinding ={结合时间filestatus}/>
                    < GridViewColumn标题=名称WIDTH =自动
                    DisplayMemberBinding ={结合文件名}/>
                    < GridViewColumn标题=大小WIDTH =自动
                     DisplayMemberBinding ={结合文件大小}/>
                    < GridViewColumn标题=系统类型WIDTH =自动
                    DisplayMemberBinding ={绑定文件类型}/>
                    < GridViewColumn标题=邮件计数WIDTH =自动
                     DisplayMemberBinding ={结合FileEmailCount}/>
                    < GridViewColumn标题=信息计数WIDTH =自动
                     DisplayMemberBinding ={结合FileInfoCount}/>
                < / GridView的>
            < /ListView.View>        < /&的ListView GT;    < /网格和GT;
< /窗GT;

视图模型code:的ViewModels \\ MainViewModel.cs

 使用Caliburn.Micro;
使用ListBox_CaliburnMicro.Model;
使用ListBox_CaliburnMicro.ViewModels;
使用系统;
使用System.Collections.Generic;
使用System.Collections.ObjectModel;
使用System.ComponentModel;
使用System.Linq的;
使用System.Text;
使用System.Threading.Tasks;
使用System.Windows.Input;命名空间ListBox_CaliburnMicro
{
    公共类MainViewModel:屏幕,INotifyPropertyChanged的
    {
        公共MainViewModel()
        {
            FillDataFile();            HelloCommand =新RelayCommand(DoHello,CanDoHello);
            AddCommand =新RelayCommand(的doAdd,CanDoAdd);
            DeleteCommand会=新RelayCommand(DoDelete,CanDoDelete);
            更新命令=新RelayCommand(DoUpdate,CanDoUpdate);
            GetSelectedItemCommand =新RelayCommand(DoGetSelectedItem,CanDoGetSelectedItem);
            GetItemXCommand =新RelayCommand(DoGetGetItemX,CanDoGetItemX);
        }        #区域文件的ListView
        私人的ObservableCollection<文件> _Files;
        公众的ObservableCollection<文件>档
        {
            {返回_Files; }
            组
            {
                _Files =价值;
                OnPropertyChanged(文件);
            }
        }        私人无效FillDataFile()
        {            _Files =新的ObservableCollection<文件>();
            的for(int i = 0; I< 100000;我++)
            {
                字符串则strName =的String.Format({0},我);
                _Files.Add(新文件(){时间filestatus =身份,文件名=则strName,文件大小= I,文件类型=类型,FileEmailCount =EMAIL_COUNT,FileInfoCount =info_count});
            }
        }        公共事件PropertyChangedEventHandler的PropertyChanged;
        公共无效OnPropertyChanged(字符串propertyName的)
        {
            如果(的PropertyChanged!= NULL)
                的PropertyChanged(这一点,新PropertyChangedEventArgs(propertyName的));
        }
        #endregion        #区域OuputText文本框
        私人字符串_OuputText = - ;
        公共字符串OuputText
        {
            得到
            {
                返回_OuputText;
            }
            组
            {
                _OuputText =价值;
                OnPropertyChanged(OuputText);
            }
        }
        #endregion        #区域ItemX文本框
        私人字符串_ItemX =0;
        公共字符串ItemX
        {
            得到
            {
                返回_ItemX;
            }
            组
            {
                INT nItem = 0;
                如果(Int32.TryParse(价值,走出nItem))
                {
                    如果(nItem&GT = _Files.Count || nItem℃,)
                        返回;
                }                _ItemX =价值;
                OnPropertyChanged(ItemX);
            }
        }
        #endregion        #区域命令按钮        #区域你好按钮
        公共RelayCommand HelloCommand {搞定;组; }
        私人布尔CanDoHello(obj对象)
        {
            返回true;
        }
        私人无效DoHello(obj对象)
        {
            System.Windows.MessageBox.Show(DoHello);
        }
        #endregion        #REGION添加按钮
        公共RelayCommand AddCommand {搞定;组; }
        私人布尔CanDoAdd(obj对象)
        {
            返回true;
        }
        私人无效的doAdd(obj对象)
        {
            字符串则strName =的String.Format({0},_Files.Count + 1);
            _Files.Add(新文件(){时间filestatus =身份,文件名=则strName,文件大小= _Files.Count,文件类型=类型,FileEmailCount =EMAIL_COUNT,FileInfoCount =info_count});
        }
        #endregion        #REGION删除按钮
        公共RelayCommand的DeleteCommand {搞定;组; }
        私人布尔CanDoDelete(obj对象)
        {
            如果(_Files.Count大于0)
                返回true;            返回false;
        }
        私人无效DoDelete(obj对象)
        {
            如果(_Files.Count大于0)
            {
                如果(的SelectedItem!= NULL)
                    _Files.Remove(的SelectedItem);
                其他
                    _Files.RemoveAt(0);
            }
        }
        #endregion        #区域更新按钮
        公共RelayCommand的UpdateCommand {搞定;组; }
        私人布尔CanDoUpdate(obj对象)
        {
            如果(_Files.Count大于0)
                返回true;            返回false;
        }
        私人无效DoUpdate(obj对象)
        {
            // VAR VVV = SelectedItems;
            如果(_Files.Count大于0)
            {
                如果(的SelectedItem!= NULL)
                    SelectedItem.FileName =更新...; //改变文件名域
                其他
                    _Files [0] .FileName =更新...;
            }            //列表<文件> FilesSelect = _Files.Where(P => p.FileID == _Files [0] .FileID).ToList();
        }
        #endregion        #区域GetSelectedItem按钮
        公共RelayCommand GetSelectedItemCommand {搞定;组; }
        私人布尔CanDoGetSelectedItem(obj对象)
        {
            如果(_Files.Count大于0)
                返回true;            返回false;
        }        公共文件的SelectedItem {搞定;组; }
        公众诠释的SelectedIndex {搞定;组; }
        私人无效DoGetSelectedItem(obj对象)
        {
            如果(_Files.Count大于0&放大器;&放大器;!的SelectedItem =空)
            {
                VAR selected_file =的SelectedItem;                OuputText =的String.Format(一个选择的索引{0} - > [{1}],SelectedIndex的,selected_file.FileID);
            }
        }
        #endregion        #区域GetItemX按钮
        公共RelayCommand GetItemXCommand {搞定;组; }
        私人布尔CanDoGetItemX(obj对象)
        {
            如果(_Files.Count大于0)
                返回true;            返回false;
        }
        私人无效DoGetGetItemX(obj对象)
        {
            INT nItem = 0;
            如果(Int32.TryParse(ItemX,出nItem))
            {
                如果(nItem< _Files.Count)
                {
                    OuputText =的String.Format(选择指数{0} - > [{1}],nItem,_Files [nItem] .FileID);
                }
            }
            其他
                OuputText = - ;
        }
        #endregion        #endregion
    }
}

ICommand的code:的ViewModels \\ RelayCommand.cs

 使用系统;
使用System.Windows.Input;命名空间ListBox_CaliburnMicro.ViewModels
{
    公共类RelayCommand:ICommand的
    {
        #区域场
        只读动作<对象> _执行;
        只读predicate<对象> _canExecute;
        #endregion        #区域构造
        公共RelayCommand(动作<对象>执行):这个(执行,NULL)
        {        }        公共RelayCommand(动作<对象>执行,predicate<对象> canExecute)
        {
            如果(执行== NULL)
                抛出新的ArgumentException(执行);            _execute =执行;
            _canExecute = canExecute;
        }
        #endregion        #区域成员
        公共BOOL CanExecute(对象参数)
        {
            return _canExecute == NULL?真:_canExecute(参数);
        }        公共事件的EventHandler CanExecuteChanged
        {
            添加{CommandManager.RequerySuggested + =价值; }
            除去{CommandManager.RequerySuggested - =价值; }
        }        公共无效执行(对象参数)
        {
            _execute(参数);
        }
        #endregion        #区域加薪
        公共无效RaiseCanExecuteChanged()
        {
            / *如果(CanExecuteChanged!= NULL)
                CanExecuteChanged(这一点,EventArgs.Empty); * /
        }
        #endregion
    }
}

示范code:型号\\ File.cs

 使用系统;
使用System.ComponentModel;命名空间ListBox_CaliburnMicro.Model
{
    公共类文件:INotifyPropertyChanged的
    {
        私人的Guid _FileID;
        公众的Guid写到FileID
        {
            得到
            {
                返回_FileID;
            }
            组
            {
                _FileID =价值;
                OnPropertyChanged(写到FileID);
            }
        }        公共事件PropertyChangedEventHandler的PropertyChanged;
        公共无效OnPropertyChanged(字符串propertyName的)
        {
            如果(的PropertyChanged!= NULL)
                的PropertyChanged(这一点,新PropertyChangedEventArgs(propertyName的));
        }        私人字符串_FileStatus;
        公共字符串时间filestatus
        {
            得到
            {
                返回_FileStatus;
            }
            组
            {
                _FileStatus =价值;
                OnPropertyChanged(时间filestatus);
            }
        }        私人字符串_filename;
        公共字符串文件名
        {
            得到
            {
                返回_filename;
            }
            组
            {
                _filename =价值;
                OnPropertyChanged(文件名);
            }
        }        公众诠释_FileSize;
        公众诠释文件大小
        {
            得到
            {
                返回_FileSize;
            }
            组
            {
                _FileSize =价值;
                OnPropertyChanged(文件大小);
            }
        }        私人字符串_FileType;
        公共字符串的文件类型
        {
            得到
            {
                返回_FileType;
            }
            组
            {
                _FileType =价值;
                OnPropertyChanged(文件类型);
            }
        }        私人字符串_FileEmailCount;
        公共字符串FileEmailCount
        {
            得到
            {
                返回_FileEmailCount;
            }
            组
            {
                _FileEmailCount =价值;
                OnPropertyChanged(FileEmailCount);
            }
        }        私人字符串_FileInfoCount;
        公共字符串FileInfoCount
        {
            得到
            {
                返回_FileInfoCount;
            }
            组
            {
                _FileInfoCount =价值;
                OnPropertyChanged(FileInfoCount);
            }
        }        公共文件()
        {
            写到FileID = Guid.NewGuid();
        }
        公共文件(字符串S1 =,字符串s2 =,INT S3 = 0,串S4 =,串S5 =,字符串S6 =)
        {
            写到FileID = Guid.NewGuid();            时间filestatus = S1;
            文件名= S2;
            文件大小= S3;
            FILETYPE = S4;
            FileEmailCount = S5;
            FileInfoCount = S6;
        }
    }
}


解决方案

您必须对这些事件挂钩到一个命令,然后在视图模型处理它们。

不使用Caliburn.Micro但也应该是EventToCommand绑定的概念。我快速搜索发现下面的链接。

HTTPS://caliburnmicro.$c$cplex.com/wikipage?标题=骗%20Sheet

的https://caliburnmicro.$c$cplex.com/ WIKIPAGE?标题=所有%20About%20Actions

<一个href=\"http://www.mindscapehq.com/blog/index.php/2012/01/24/caliburn-micro-part-3-more-about-events-and-parameters/\" rel=\"nofollow\">http://www.mindscapehq.com/blog/index.php/2012/01/24/caliburn-micro-part-3-more-about-events-and-parameters/

如果我没有得到你的问题正确,请让我知道...

心连心

I wrote a program with MVVM (C#) and XAML using Caliburn.Micro library, how can i:

  • get all selected items (not only one item)?
  • get selected change event?
  • sort items by clicking header columns?

Any help would be appreciated.

GUI code: 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 Command="{Binding HelloCommand}" Content="Hello ..." HorizontalAlignment="Left" Margin="10,36,0,0" VerticalAlignment="Top" Width="75" ToolTip="Test" Background="White" Foreground="Black" Height="22" />
        <Button Content="Add" Command="{Binding AddCommand}" HorizontalAlignment="Left" Margin="10,8,0,0" VerticalAlignment="Top" Width="75" ToolTip="Add item end of list" Background="White" Foreground="Black" Height="22"/>
        <Button Content="Delete" Command="{Binding DeleteCommand}" HorizontalAlignment="Left" Margin="96,8,0,0" VerticalAlignment="Top" Width="75" ToolTip="Delete first item from list" Background="White" Foreground="Black" Height="22"/>
        <Button Content="Update" Command="{Binding UpdateCommand}" HorizontalAlignment="Left" Margin="184,8,0,0" VerticalAlignment="Top" Width="75" ToolTip="Update first item from list" Background="White" Foreground="Black" Height="22"/>
        <Button Content="GetSelectedItem" Command="{Binding GetSelectedItemCommand}" HorizontalAlignment="Left" Margin="271,8,0,0" VerticalAlignment="Top" Width="95" ToolTip="get selected item from list" Background="White" Foreground="Black" Height="22"/>
        <Button Content="GetItem:" Command="{Binding GetItemXCommand}" HorizontalAlignment="Left" Margin="377,8,0,0" VerticalAlignment="Top" Width="75" ToolTip="get item x, from list" Background="White" Foreground="Black" Height="22"/>

        <TextBox x:Name="ItemX" Text="0" HorizontalAlignment="Left" Height="20" Margin="455,10,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="70"/>

        <TextBlock x:Name="OuputText" HorizontalAlignment="Left" Height="20" Margin="165,40,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="415" Foreground="#FFFF0909"/>
        <Label Content="Output:"  HorizontalAlignment="Left" Height="28" Margin="100,35,0,0" VerticalAlignment="Top" Width="55"/>

        <ListView Grid.Column="1" x:Name="FileListView" SelectionMode="Extended" SelectedItem="{Binding SelectedItem}" SelectedIndex="{Binding SelectedIndex}"  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>

ViewModel code: 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<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 < 100000; i++)
            {
                string strName = string.Format("{0}", i);
                _Files.Add(new File() { FileStatus = "status", FileName = strName, FileSize = i, 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 OuputText textbox
        private string _OuputText = "-";
        public string OuputText
        {
            get
            {
                return _OuputText;
            }
            set
            {
                _OuputText = value;
                OnPropertyChanged("OuputText");
            }
        }
        #endregion

        #region ItemX textbox
        private string _ItemX = "0";
        public string ItemX
        {
            get
            {
                return _ItemX;
            }
            set
            {
                int nItem = 0;
                if (Int32.TryParse(value, out nItem))
                {
                    if (nItem >= _Files.Count || nItem < 0)
                        return;
                }

                _ItemX = value;
                OnPropertyChanged("ItemX");
            }
        }
        #endregion

        #region command button

        #region Hello button
        public RelayCommand HelloCommand { get; set; }
        private bool CanDoHello(object obj)
        {
            return true;
        }
        private void DoHello(object obj)
        {
            System.Windows.MessageBox.Show("DoHello");
        }
        #endregion

        #region Add button
        public RelayCommand AddCommand { get; set; }
        private bool CanDoAdd(object obj)
        {
            return true;
        }
        private void DoAdd(object obj)
        {
            string strName = string.Format("{0}", _Files.Count + 1);
            _Files.Add(new File() { FileStatus = "status", FileName = strName, FileSize = _Files.Count, FileType = "type", FileEmailCount = "email_count", FileInfoCount = "info_count" });
        }
        #endregion

        #region Delete button
        public RelayCommand DeleteCommand { get; set; }
        private bool CanDoDelete(object obj)
        {
            if (_Files.Count > 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<File> 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 < _Files.Count)
                {
                    OuputText = string.Format("select index {0} -> [{1}]", nItem, _Files[nItem].FileID);
                }
            }
            else
                OuputText = "-";
        }
        #endregion

        #endregion
    }
}

ICommand code: ViewModels \ RelayCommand.cs

using System;
using System.Windows.Input;

namespace ListBox_CaliburnMicro.ViewModels
{
    public class RelayCommand : ICommand
    {
        #region field
        readonly Action<object> _execute;
        readonly Predicate<object> _canExecute;
        #endregion

        #region constructors
        public RelayCommand(Action<object> execute) : this(execute, null)
        {

        }

        public RelayCommand(Action<object> execute, Predicate<object> 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 code: 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;
        }
    }
}

解决方案

You have to hook up these events to a command and process them in the viewmodel.

Not using Caliburn.Micro but there should also be a concept for EventToCommand Binding. I quick search revealed the following links.

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/

if I did not get your question correct please let me know...

HTH

这篇关于我怎样才能在C#中选择项目(S)和事件的ListView与Caliburn.Micro?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆