C# 将命令参数从按钮接受为DelegateCommand
我有一个items控件,我正在向它传递一个可观察的对象集合,并将元素显示为按钮。我正在使用delegates命令捕获视图模型中的按钮单击 我想知道如何知道单击了哪个按钮。我希望能够将与按钮关联的对象传递给我的VM 我的xaml:C# 将命令参数从按钮接受为DelegateCommand,c#,wpf,xaml,visual-studio-2015,prism,C#,Wpf,Xaml,Visual Studio 2015,Prism,我有一个items控件,我正在向它传递一个可观察的对象集合,并将元素显示为按钮。我正在使用delegates命令捕获视图模型中的按钮单击 我想知道如何知道单击了哪个按钮。我希望能够将与按钮关联的对象传递给我的VM 我的xaml: <ItemsControl x:Name="list" ItemsSource="{Binding ChemList}"> //ChemList is observable collection of objects <ItemsControl
<ItemsControl x:Name="list" ItemsSource="{Binding ChemList}"> //ChemList is observable collection of objects
<ItemsControl.ItemTemplate>
<DataTemplate>
<Button Margin="5"
Command="{Binding ElementName=list,Path=DataContext.OnBtnSelect}"
CommandParameter="{Binding}">
<Button.Content>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding name}"/>
<TextBlock Text=" "/>
<TextBlock Text="{Binding num}"/>
</StackPanel>
</Button.Content>
</Button>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
DelegateCommand应该接受
Action<object>
动作
作为构造函数参数。public DelegateCommand OnBtnSelect{get;private set;}
public DelegateCommand<object> OnBtnSelect { get; private set; }
public void OnSelect(object args)
{
//If your binding is correct args should contains the payload of the event
}
//In the constructor
OnBtnSelect = new DelegateCommand<object>(OnSelect);
公共void OnSelect(对象参数)
{
//如果绑定正确,则args应包含事件的有效负载
}
//在构造函数中
OnBtnSelect=新的DelegateCommand(OnSelect);
你在用Prism吗?是的,我在用prismOk,请看我的帖子
public DelegateCommand<object> OnBtnSelect { get; private set; }
public void OnSelect(object args)
{
//If your binding is correct args should contains the payload of the event
}
//In the constructor
OnBtnSelect = new DelegateCommand<object>(OnSelect);