如何更新在C#中的ListView从与Caliburn.Micro项目? [英] How can i update items from ListView with Caliburn.Micro in C#?
问题描述
我写了一个简单的程序与MVVM(C#)和XAML使用Caliburn.Micro库,
但是有些行为没有工作:
- 更新项目内容
- 获得选择的项目
- 获取特定项目
- 将向上和向下移动记录
- 启用或禁用按钮时添加按钮,点击
任何帮助将是AP preciated。
我的GUI code:
//浏览\\ MainView.xaml
<窗口x:类=ListBox_CaliburnMicro.MainView
的xmlns =http://schemas.microsoft.com/winfx/2006/xaml/$p$psentation
的xmlns:X =http://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> <按钮内容=add命令={结合AddCommand}Grid.Column =1的HorizontalAlignment =左保证金=10,37,0,0VerticalAlignment =评出的WIDTH =75工具提示=添加列表的项目结束背景=白前景=黑HEIGHT =22/>
<按钮内容=删除命令={结合的DeleteCommand}Grid.Column =1的HorizontalAlignment =左保证金=96,37,0,0VerticalAlignment =评出的WIDTH =75工具提示=从列表中删除第一个项目后台=白前景=黑HEIGHT =22/>
<按钮内容=更新命令={结合的UpdateCommand}Grid.Column =1的HorizontalAlignment =左保证金=184,37,0,0VerticalAlignment =评出的WIDTH =75工具提示=从列表更新的第一个项目后台=白前景=黑HEIGHT =22/>
<按钮内容=GetSelectedItem命令={结合GetSelectedItemCommand}Grid.Column =1的HorizontalAlignment =左保证金=271,37,0,0VerticalAlignment =评出的WIDTH =95工具提示=从列表中选择获取项目的背景=白前景=黑HEIGHT =22/>
<按钮内容=的GetItem:命令={结合GetItemXCommand}Grid.Column =1的HorizontalAlignment =左保证金=377,37,0,0VerticalAlignment =评出的WIDTH =75工具提示=拿到项x,从列表背景=白前景=黑HEIGHT =22/> <文本框Grid.Column =1×:NAME =ItemX文本=0的HorizontalAlignment =左身高=24保证金=457,35,0,0TextWrapping =包装VerticalAlignment = 顶级WIDTH =70/> < ListView控件Grid.Column =1×:NAME =FileListView的ItemsSource ={绑定路径=文件}VerticalAlignment =拉伸HorizontalContentAlignment =拉伸保证金=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;
我的模式是:
//型号\\ File.cs
使用系统;命名空间ListBox_CaliburnMicro.Model
{
公共类文件
{
私人的Guid _FileID;
公众的Guid写到FileID
{
得到
{
返回_FileID;
}
组
{
_FileID =价值;
}
}
公共字符串时间filestatus {搞定;组; }
公共字符串文件名{获得;组; }
公共字符串文件大小{搞定;组; }
公共字符串的文件类型{搞定;组; }
公共字符串FileEmailCount {搞定;组; }
公共字符串FileInfoCount {搞定;组; } 公共文件()
{
写到FileID = Guid.NewGuid();
}
公共文件(字符串S1 =,字符串s2 =,字符串S3 =,字符串S4 =,串S5 =,字符串S6 =)
{
写到FileID = Guid.NewGuid(); 时间filestatus = S1;
文件名= S2;
文件大小= S3;
FILETYPE = S4;
FileEmailCount = S5;
FileInfoCount = S6;
}
}
}
我的命令处理程序code:
//的ViewModels \\ CommandHandler.cs
使用系统;
使用System.Windows.Input;命名空间ListBox_CaliburnMicro.ViewModels
{
公共类CommandHandler:ICommand的
{
私人System.Action _action;
私人布尔_canExecute;
公共CommandHandler(System.Action行动,布尔canExecute)
{
_action =行动;
_canExecute = canExecute;
}
公共BOOL CanExecute(对象参数)
{
返回_canExecute;
} 公共事件的EventHandler CanExecuteChanged;
公共无效执行(对象参数)
{
_行动();
}
}
}
和我的视图模型:
//的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(); _canAddCommandExecute = TRUE;
_canDeleteCommandExecute = FALSE;
_canUpdateCommandExecute = FALSE;
_canGetSelectedItemCommandExecute = FALSE;
_canGetItemXCommandExecute = FALSE;
} #区域文件的ListView
私人的ObservableCollection<文件> _Files;
公众的ObservableCollection<文件>档
{
{返回_Files; }
组
{
_Files =价值;
OnPropertyChanged(文件);
}
} 私人无效FillDataFile()
{
_Files =新的ObservableCollection<文件>();
的for(int i = 0;我℃下,我++)
{
_Files.Add(新文件(){时间filestatus =身份,文件名=名,文件大小=大小,文件类型=类型,FileEmailCount =EMAIL_COUNT,FileInfoCount =info_count});
}
} 公共事件PropertyChangedEventHandler的PropertyChanged;
公共无效OnPropertyChanged(字符串propertyName的)
{
如果(的PropertyChanged!= NULL)
的PropertyChanged(这一点,新PropertyChangedEventArgs(propertyName的));
}
#endregion #区域ItemX文本框
私人字符串_ItemX =0;
公共字符串ItemX
{
得到
{
返回_ItemX;
}
组
{
_ItemX =价值;
}
}
#endregion #区域命令按钮 #REGION添加按钮,命令
私人的ICommand _AddCommand;
公众的ICommand AddCommand
{
得到
{
返回_AddCommand? (_AddCommand =新CommandHandler(()=>的addAction(),_canAddCommandExecute));
}
} 静国际nIndex = 0; 私人布尔_canAddCommandExecute;
公共无效的addAction()
{
字符串则strName =的String.Format({0},参数nIndex ++);
_Files.Add(新文件(){时间filestatus =身份,文件名=则strName,文件大小=大小,文件类型=类型,FileEmailCount =EMAIL_COUNT,FileInfoCount =info_count}); //启用操作按钮,
//!但不措辞!
_canAddCommandExecute = TRUE;
_canDeleteCommandExecute = TRUE;
_canUpdateCommandExecute = TRUE;
_canGetSelectedItemCommandExecute = TRUE;
_canGetItemXCommandExecute = TRUE;
}
#endregion #REGION删除与命令按钮
私人的ICommand _DeleteCommand;
公众的ICommand的DeleteCommand
{
得到
{
返回_DeleteCommand? (_DeleteCommand =新CommandHandler(()=> DeleteAction(),_canDeleteCommandExecute));
}
} 私人布尔_canDeleteCommandExecute;
公共无效DeleteAction()
{
如果(_Files.Count大于0)
_Files.RemoveAt(0);
}
#endregion 使用命令#区域更新按钮
私人的ICommand _UpdateCommand;
公众的ICommand的UpdateCommand
{
得到
{
返回_UpdateCommand? (_UpdateCommand =新CommandHandler(()=> UpdateAction(),_canUpdateCommandExecute));
}
} 私人布尔_canUpdateCommandExecute;
公共无效UpdateAction()
{
如果(_Files.Count大于0)
{
//更新文件名域
_files [0] .FileName =+; //!但没有工作! //列表<文件> FilesSelect = _Files.Where(P => p.FileID == _Files [0] .FileID).ToList();
}
}
#endregion 使用命令#区域GetSelectedItem按钮
私人的ICommand _GetSelectedItemCommand;
公众的ICommand GetSelectedItemCommand
{
得到
{
返回_GetSelectedItemCommand? (_GetSelectedItemCommand =新CommandHandler(()=> GetSelectedItemAction(),_canGetSelectedItemCommandExecute));
}
} 私人布尔_canGetSelectedItemCommandExecute;
公共无效GetSelectedItemAction()
{
如果(_Files.Count大于0)
{
// ...得到的ListView所选项目
}
}
#endregion 使用命令#区域GetItemX按钮
私人的ICommand _GetItemXCommand;
公众的ICommand GetItemXCommand
{
得到
{
返回_GetItemXCommand? (_GetItemXCommand =新CommandHandler(()=> GetItemXAction(),_canGetItemXCommandExecute));
}
} 私人布尔_canGetItemXCommandExecute;
公共无效GetItemXAction()
{
如果(_Files.Count大于0)
{
// ...获取item'X',在TextBox控件输入
}
}
#endregion #endregion
}
}
更新项目的内容。
要看到在您的视图模型的更新的项目,那么你应该在每个模型的财产实施 INotifyPropertyChanged的
事件。例如:
公共类文件:INotifyPropertyChanged的
{
私人的Guid _FileID;
公众的Guid写到FileID
{
得到
{
返回_FileID;
}
组
{
_FileID =价值;
OnPropertyChanged(写到FileID);
}
} 公共事件PropertyChangedEventHandler的PropertyChanged;
公共无效OnPropertyChanged(字符串propertyName的)
{
如果(的PropertyChanged!= NULL)
{
的PropertyChanged(这一点,新PropertyChangedEventArgs(propertyName的));
}
}
}
获取选定的项目。
创建视图模型一个属性用于保存选定的项目,例如文件
:
公开文件SelectedFile {搞定;组; }
然后将其绑定的SelectedItem
的的ListView
此属性:
< ListView控件名称=UserGrid的SelectedItem ={结合SelectedFile}
的ItemsSource ={绑定路径=文件}/>
您已经实施 SelectedFile
属性后,你可以采取这样的值:
如果(SelectedFile!= NULL)
VAR exactFile = SelectedFile;
启用或禁用按钮时添加按钮点击。
您有两种方式:随着卡利和不卡利
随着CaliburnMicro:
CaliburnMicro有自己的机制来处理按钮点击
事件。例如:
<按钮内容=ShowPageTwo>
< I:Interaction.Triggers>
< I:的EventTrigger事件名称=点击>
< CAL:ActionMessage的方法名=ShowPageTwo/>
< /我:&的EventTrigger GT;
< /我:Interaction.Triggers>
< /按钮>
要查看更多详情关于实施,请参阅在$官方指南C $ CPLEX 。
无CaliburnMicro:
首先,你应该创建 RelayCommand
类通过调用委托给它的中继功能,其他对象:
公共类RelayCommand:ICommand的
{
#地区字段 只读动作<对象> _执行;
只读predicate<对象> _canExecute; #endregion //领域 #区域构造 公共RelayCommand(动作<对象>执行)
:这个(执行,NULL)
{
} 公共RelayCommand(动作<对象>执行,predicate<对象> canExecute)
{
如果(执行== NULL)
抛出新的ArgumentNullException(执行); _execute =执行;
_canExecute = canExecute;
}
#endregion //构造 #区域成员的ICommand 公共BOOL CanExecute(对象参数)
{
返回_canExecute == NULL?真:_canExecute(参数);
} 公共事件的EventHandler CanExecuteChanged
{
添加{CommandManager.RequerySuggested + =价值; }
除去{CommandManager.RequerySuggested - =价值; }
} 公共无效执行(对象参数)
{
_execute(参数);
} #endregion //成员的ICommand
}
然后创建您的视图模型的属性。例如:
公共类YourViewModel
{
公共RelayCommand YourCommand {搞定;组; }
公共YourViewModel()
{
YourCommand =新RelayCommand(DoSmth,CanDoSmth);
} 私人无效DoSmth(obj对象)
{
Message.Box(你好,从视图模型);
} 私人布尔CanDoSmth(obj对象)
{
//你可以在这里实现自己的逻辑。但默认情况下它应该是
//设置为true
返回true;
}
}
和XAML应该是这样的:
<按钮内容=点击我!命令={结合LoginCommand}/>
I wrote a simple program with MVVM (C#) and XAML using Caliburn.Micro library, But some actions didn't work:
- update an item content
- get selected item
- get specific item
- move up and move down records
- enable or disable buttons when Add button clicked
Any help would be appreciated.
My 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 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>
My model is:
// 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;
}
}
}
My command handler code:
// 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();
}
}
}
And my 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
}
}
Update an item content.
To see an updated item of model in your view, then you should implement INotifyPropertyChanged
event in EACH property of your model. For example:
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));
}
}
}
Get selected item.
Create a Property in the ViewModel for saving the selected item, for instance File
:
public File SelectedFile { get; set; }
then bind SelectedItem
of the ListView
to this property:
<ListView Name="UserGrid" SelectedItem="{Binding SelectedFile}"
ItemsSource="{Binding Path=Files}"/>
After you've implemented SelectedFile
property, you can take the value like that:
if(SelectedFile!=null)
var exactFile=SelectedFile;
Enable or disable buttons when Add button clicked. You have two ways: With Caliburn and without Caliburn
With CaliburnMicro:
CaliburnMicro has own mechanism to handle Button Click
event. For instance:
<Button Content="ShowPageTwo">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<cal:ActionMessage MethodName="ShowPageTwo" />
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>
To see more details about implementation, please see official guide at CodePlex.
Without CaliburnMicro:
At first you should create RelayCommand
class to relay its functionality to other objects by invoking delegates:
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
}
Then create a property in your viewModel. For instance:
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;
}
}
And XAML should be look like:
<Button Content="Click me!" Command="{Binding LoginCommand}"/>
这篇关于如何更新在C#中的ListView从与Caliburn.Micro项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!