下載app免費(fèi)領(lǐng)取會(huì)員
網(wǎng)友投稿
更多// 摘要:
// Defines a command.
public interface ICommand
{
//
// 摘要:
// Occurs when changes occur that affect whether or not the command should execute.
event EventHandler CanExecuteChanged;
//
// 摘要:
// Defines the method that determines whether the command can execute in its current
// state.
//
// 參數(shù):
// parameter:
// Data used by the command. If the command does not require data to be passed,
// this object can be set to null.
//
// 返回結(jié)果:
// true if this command can be executed; otherwise, false.
bool CanExecute(object parameter);
//
// 摘要:
// Defines the method to be called when the command is invoked.
//
// 參數(shù):
// parameter:
// Data used by the command. If the command does not require data to be passed,
// this object can be set to null.
void Execute(object parameter);
上面這個(gè)是ICommand包含的類容,通過(guò)一個(gè)小例子來(lái)說(shuō)明她的使用方法,
首先在Window里添加一個(gè)Button和一個(gè)Textbox,給Button綁定一個(gè)命令,給Text box綁定一個(gè)int數(shù)字,當(dāng)int 數(shù)字小于0是Button不可用。
首先創(chuàng)建命令:
public class OK_Command : ICommand
{
private ViewModel _vm = null;
public OK_Command(ViewModel vm)
{
_vm = vm;
}
public event EventHandler CanExecuteChanged;
public void RaiseCanExecuteChanged()
{
if(CanExecuteChanged!=null)
{
CanExecuteChanged(this, EventArgs.Empty);
}
}
public bool CanExecute(object parameter)
{
if (_vm.Num < 0)
return false;
return true;
}
public void Execute(object parameter)
{
MessageBox.Show("OK");
}
}
創(chuàng)建View Model:
public class ViewModel : INotifyPropertyChanged
{
private int _num = 0;
public int Num
{
get
{
return _num;
}
set
{
_num = value;
RaisePropertyChanged(nameof(Num));
RaisePropertyChanged((nameof(OK_Command)));
}
}
private OK_Command _okCommand = null;
public OK_Command OK_Command
{
get
{
if(_okCommand ==null)
{
_okCommand = new OK_Command(this);
}
_okCommand.RaiseCanExecuteChanged();
return _okCommand;
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void RaisePropertyChanged(string name)
{
if(PropertyChanged!=null)
{
PropertyChanged(this, new PropertyChangedEventArgs(name));
}
}
}
在Window里添加Button和TextBox 并設(shè)置綁定路徑
<Window x:Class="ICommandDemo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:ICommandDemo"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Button x:Name="button" Command="{Binding Path=OK_Command}" Content="Button" HorizontalAlignment="Left" Margin="334,268,0,0" VerticalAlignment="Top" Width="75"/>
<TextBox x:Name="textBox" HorizontalAlignment="Left" Height="23" Margin="288,176,0,0" TextWrapping="Wrap" Text="{Binding Path= Num,UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Top" Width="120"/>
</Grid>
</Window>
最后在Window啟動(dòng)設(shè)置Data Context
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = new ViewModel();
}
}
本文版權(quán)歸腿腿教學(xué)網(wǎng)及原創(chuàng)作者所有,未經(jīng)授權(quán),謝絕轉(zhuǎn)載。
推薦專題