C# 使用Blend SDK事件触发器触发与相关列表视图项MVVM一起单击的按钮
我正在尝试使用Blend的事件触发器触发listview项的按钮单击事件,它应该能够工作,以便不必为要引用的相关行选择项 我的代码是C# 使用Blend SDK事件触发器触发与相关列表视图项MVVM一起单击的按钮,c#,wpf,mvvm,wpf-controls,C#,Wpf,Mvvm,Wpf Controls,我正在尝试使用Blend的事件触发器触发listview项的按钮单击事件,它应该能够工作,以便不必为要引用的相关行选择项 我的代码是 Public void MyCommand(object obj) { // the tag of this has the search type ListViewItem item = obj as ListViewItem; // do my dreary domain work... } 我的xaml是 <ListView
Public void MyCommand(object obj)
{
// the tag of this has the search type
ListViewItem item = obj as ListViewItem;
// do my dreary domain work...
}
我的xaml是
<ListView ItemsSource="{Binding Path=SystemSetupItems}"
SelectedItem="{Binding Selected, Mode=TwoWay}"
MinHeight="120" >
<ListView.View>
<GridView>
<GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}" />
<GridViewColumn Header="Description" DisplayMemberBinding="{Binding Description}" />
<GridViewColumn>
<GridViewColumn.CellTemplate>
<DataTemplate>
<Button >
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseClick">
<i:InvokeCommandAction CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=ListViewItem, AncestorLevel=1}}" Command="{Binding MyCommand}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
但是这根本不起作用,或者我可以在我的xaml按钮定义中这样做
<GridViewColumn.CellTemplate>
<DataTemplate>
<Button Command="{Binding OpenWorkSpaceCommand}" CommandParameter="{Binding Path=Name}" Content="Edit..." DataContext="{Binding DataContext, RelativeSource={RelativeSource FindAncestor, AncestorType=ListView}}" >
</Button>
</DataTemplate>
</GridViewColumn.CellTemplate>
但是这需要事先选择listview项,这不是我想要的行为。对于我的DataGrid,我在每个项上都有一个使用单元格模板的按钮。每个项目都是类型为
的对象。在my Dine.cs文件中,我有这样一个事件定义:
public Meal()
{
RemoveMealCommand = new RelayCommand(() => RemoveMealCommandExecute());
}
public RelayCommand RemoveMealCommand
{
get;
set;
}
public delegate void RemoveMealEventHandler(object sender, EventArgs e);
public event RemoveMealEventHandler RemoveMealEvent;
private void RemoveMealCommandExecute()
{
RemoveMealEvent(this, null);
}
在我的viewmodel中,我可以为列表中的每顿饭添加一个处理程序。对于我的xaml按钮,我只是将命令设置为餐点的RelayCommand
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Command="{Binding Path=RemoveMealCommand}">
<Image Width="13" Height="13" Source="/Images/delete-icon.png"/>
</Button>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
现在,当您单击按钮时,MEIN负责触发事件,viewmodel负责处理它。我尝试此操作并得到一个“委托”系统。操作“不接受0个参数”编译错误您得到的错误是哪一行?我猜是ViewModel中的事件处理程序吗?哦,它在这一行RemoveMealCommand=new RelayCommand(()=>RemoveMealCommand=Execute());整个代码是。。。CheeseFactoryCommand=new RelayCommand(()=>CheeseFactoryExecute());public RelayCommand CheeseFactoryCommand{get;set;}public委托void CheeseFactoryEventHandler(对象发送方,EventArgs e);公共事件CheeseFactoryEventHandler CheeseFactoryEvent;private void CheeseFactoryExecute(对象发送方){CheeseFactoryEvent(this,null);}我正在将MVVMLight库用于我的RelayCommands。您的库可能有不同的实现。我发布的代码就是一个对我有用的例子。我希望您可以使用它来了解如何将事件附加到每个SystemSetupItems
。我不认为把我的代码复制到你的代码中会有帮助。