1 <Window x:Class="NoteBook.MainWindow" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" 5 Title="Surface" Height="350" Width="525"> 6 <StackPanel> 7 <TextBox 8 x:Name="textBox" 9 TextWrapping="Wrap" 10 AcceptsReturn="True" 11 VerticalScrollBarVisibility="Visible" 12 Background="GreenYellow" 13 Height="250" 14 FontFamily="Microsoft YaHei" 15 FontSize="16" 16 Text="{Binding Input}"> 17 <i:Interaction.Triggers> 18 <i:EventTrigger EventName="PreviewMouseLeftButtonUp" > 19 <i:InvokeCommandAction Command="{Binding selectCommand}" 20 CommandParameter="{Binding SelecetedText, 21 ElementName=textBox}" > 22 </i:InvokeCommandAction> 23 </i:EventTrigger> 24 </i:Interaction.Triggers> 25 </TextBox> 26 </StackPanel> 27 </Window>
在相应的viewmodel类中怎么得到CommandParameter的值?
在viewmodel类里是使用ICommand接口来实现Command的,接口定义了void Execute(object parameter)方法,其中的parameter就是在界面绑定的CommandParameter的值
1 class MainWindowViewModel:NotificationObject{ 2 public DelegateCommand selectCommand { get; set; } 3 4 private void selectTextDis() 5 { 6 7 MessageBox.Show(""); 8 } 9 10 public MainWindowViewModel() 11 { 12 selectCommand = new DelegateCommand(new Action(selectTextDis)); 13 } 14 }
我是仿照demo中写的,这样写是不是就得不到参数了?
要重新定义ICommand selectCommand才行?
@神都大理寺: 不知道你是不是使用的Prism框架,如果是,那么DelegateCommand类型是一个无参数的Command,你应该用DelegateCommand<T>类型,也就是使用下面的代码:
class MainWindowViewModel:NotificationObject{ public DelegateCommand<object> selectCommand { get; set; } private void selectTextDis(object param) { MessageBox.Show(""); } public MainWindowViewModel() { selectCommand = new DelegateCommand<object>(selectTextDis); } }