本章讲述:在MVVMLight中,简单的Command命令绑定示例;
前端代码示例:两种绑定方式
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" <!--引用库-->
<Window.DataContext>
<vm:MainWindowVM/>
</Window.DataContext>
<StackPanel HorizontalAlignment="Left">
<Button Margin="10" Width="100" Height="30" Content="MVVM1" Command="{Binding ShowMsgCommand}"></Button>
<Button Margin="10" Width="100" Height="30" Content="MVVM2" Command="{Binding ButtonClickCommand}"></Button>
<Button Margin="10" Width="100" Height="30" Content="MVVM2" Command="{Binding CloseCmd}"
CommandParameter="{Binding RelativeSource={x:Static RelativeSource.Self}}"/>
<Button Style="{StaticResource ViewImagebtnStyleKey}" Margin="5 0 0 0" ToolTip="" Command="{Binding DelCommand,RelativeSource={RelativeSource TemplatedParent}}" CommandParameter="{Binding}">
<Image Height="20" Width="20" Source="/DSViewer;component/Images/ImageList/delete.png" Stretch="Fill"/>
</Button>
<Button Name="btn" Content="Button" Height="30" HorizontalAlignment="Left" Margin="10" VerticalAlignment="Top" Width="100">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<i:InvokeCommandAction Command="{Binding Command1}" CommandParameter="10" />
</i:EventTrigger>
<i:EventTrigger EventName="MouseMove">
<i:InvokeCommandAction Command="{Binding Command2}" CommandParameter="{Binding ElementName=btn}" />
</i:EventTrigger>
<i:EventTrigger EventName="MouseLeave">
<i:InvokeCommandAction Command="{Binding Command_MousLeave}" CommandParameter="{Binding ElementName=btn}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>
<Image Name="imge" Width="25" Height="25" HorizontalAlignment="Center" Margin="10" Source="/CommandTest;component/Images/u1285.png">
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseEnter">
<i:InvokeCommandAction Command="{Binding Command_MouseEnter}" CommandParameter="{Binding ElementName=imge}"/>
</i:EventTrigger>
<i:EventTrigger EventName="MouseLeave">
<i:InvokeCommandAction Command="{Binding Command_ImageMouseLeave}" CommandParameter="{Binding ElementName=imge}"/>
</i:EventTrigger>
<i:EventTrigger EventName="MouseLeftButtonUp">
<i:InvokeCommandAction Command="{Binding Command_Click}" CommandParameter="{Binding ElementName=imge}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</Image>
</StackPanel>
“CommandParameter="{Binding RelativeSource={x:Static RelativeSource.Self}}" ”表示:绑定的参数是自身;
“CommandParameter="{Binding ElementName=btn}" ” 表示:绑定一个控件,btn是控件名称;
“CommandParameter="10" ” 表示:绑定参数是10;
后端逻辑代码
private ICommand m_CloseCmd;
public ICommand CloseCmd
{
get
{
return m_CloseCmd ?? (m_CloseCmd = new RelayCommand<Button>((bnt) =>
{
bnt.Content = "Close";
MessageBox.Show("Close Window!");
App.Current.MainWindow.Close();
}));
}
}
private ICommand m_Command1;
public ICommand Command1
{
get
{
return m_Command1 ?? (m_Command1 = new RelayCommand<string>((str) =>
{
string param = str;
MessageBox.Show("参数 :" + param);
}));
}
}
private ICommand m_Command2;
public ICommand Command2
{
get
{
return m_Command2 ?? (m_Command2 = new RelayCommand<Button>((btn) =>
{
btn.Content = "setting";
}));
}
}
private ICommand m_Command_MousLeave;
public ICommand Command_MousLeave
{
get
{
return m_Command_MousLeave ?? (m_Command_MousLeave = new RelayCommand<Button>((bnt) =>
{
bnt.Content = "Button";
}));
}
}
private ICommand m_Command_MouseEnter;
public ICommand Command_MouseEnter
{
get
{
return m_Command_MouseEnter ?? (m_Command_MouseEnter = new RelayCommand<Image>((img) =>
{
img.Source = BitmapFromUri(new Uri("pack://application:,,,/CommandTest;component/Images/u1128.png"));
}));
}
}
private ICommand m_Command_ImageMouseLeave;
public ICommand Command_ImageMouseLeave
{
get
{
return m_Command_ImageMouseLeave ?? (m_Command_ImageMouseLeave = new RelayCommand<Image>((img) =>
{
img.Source = BitmapFromUri(new Uri("pack://application:,,,/CommandTest;component/Images/u1285.png"));
}));
}
}
private ICommand m_Command_Click;
public ICommand Command_Click
{
get
{
return m_Command_Click ?? (m_Command_Click = new RelayCommand<Image>((img) =>
{
MessageBox.Show("Image click!");
}));
}
}
static object lockObj = new object();
public static System.Windows.Media.ImageSource BitmapFromUri(Uri source)
{
var bitmap = new System.Windows.Media.Imaging.BitmapImage();
try
{
var temp = new System.Windows.Media.Imaging.BitmapImage(source);
temp = null;
lock (lockObj)
{
bitmap.BeginInit();
bitmap.UriSource = source;
bitmap.CacheOption = System.Windows.Media.Imaging.BitmapCacheOption.OnLoad;
bitmap.EndInit();
}
}
catch { }
return bitmap;
}