使用事件命令带参数。
<Button Content="Add">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<cmd:EventToCommand Command="{Binding AddClickCommand, Mode=OneWay}"
PassEventArgsToCommand="True" />
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>
这里的命名空间“i”指的是 xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
和
xmlns:cmd="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.SL4"
这是galasoft MVVM轻工具包(谷歌下载)
在viewModel中添加类似的代码
public ICommand AddClickCommand { get; set; }
AddClickCommand = new RelayCommand<string>((e) =>
{
this.BeginInsert();
});
希望这对你有用。
我以为你会从我的回答开始。
我想说服你的是
<your:YourGrid x:Name="yourGrid">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Loaded">
<cmd:EventToCommand Command="{Binding GridLoadedCommand, Mode=OneWay}"
PassEventArgsToCommand="True" />
</i:EventTrigger>
</i:Interaction.Triggers>
</your:YourGrid>
您需要做的就是在 LoadedCommand 中。
YourGrid grid ;
public ICommand GridLoadedCommand { get; set; }
GridLoadedCommand = new RelayCommand<RoutedEventHandler>((s, e) =>
{
grid = (YourGrid) s;
});
public ICommand AddClickCommand { get; set; }
AddClickCommand = new RelayCommand<string>((e) =>
{
grid.BeginInsert();
});
Now what you say about this.