如何在silverlight和MVVM中从datagrid内的按钮触发事件
我在datagrid的第一列有一个按钮。我正在使用MVVM并尝试将命令绑定到ViewModel中的命令,但当我单击每行中的按钮时,它不起作用(它不调用ViewModel中的命令),但如果我将该按钮移出datagrid,它就会正常工作 如何在MVVM中从datagrid内的按钮触发事件 更新1: XAML的代码是:如何在silverlight和MVVM中从datagrid内的按钮触发事件,silverlight,mvvm,Silverlight,Mvvm,我在datagrid的第一列有一个按钮。我正在使用MVVM并尝试将命令绑定到ViewModel中的命令,但当我单击每行中的按钮时,它不起作用(它不调用ViewModel中的命令),但如果我将该按钮移出datagrid,它就会正常工作 如何在MVVM中从datagrid内的按钮触发事件 更新1: XAML的代码是: <datagrid:DataGridTemplateColumn.CellTemplate> <DataTemplate> <St
<datagrid:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal"
VerticalAlignment="Center">
<Button x:Name="button" Content="View" Margin="5" DataContext="{StaticResource XDataContext}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<i:InvokeCommandAction Command="{Binding ViewOrganizationCommand}"
CommandParameter="{Binding ElementName=dtgOrganizations, Path=SelectedItem}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>
</StackPanel>
</DataTemplate>
</datagrid:DataGridTemplateColumn.CellTemplate>
public ViewModelCommand ViewOrganizationCommand { get; set; }
与其使用EventTrigger,为什么不直接绑定到Button.Command,就像这样
<Button
...other properties...
Command="{Binding ViewOrganizationsCommand}"
CommandParameter="{Binding}"/>
这将绑定命令,并将CommandParameter设置为按钮的DataContext,这可能是绑定参数的好方法。如果不是,只需将CommandParameter绑定到其他可以帮助您唯一标识所单击的特定行的对象即可。使用事件触发器的命令绑定看起来很好(我总是这样做的), 但我怀疑您的命令从未分配给(您使用的是自动属性) 我通常是这样做的:
private ViewModelCommand viewOrganizationCommand;
public ViewModelCommand ViewOrganizationCommand
{
get
{
if (viewOrganizationCommand == null)
viewOrganizationCommand = new ViewModelCommand(Action, CanDoIt);
return viewOrganizationCommand;
}
}
尝试将
按钮的DataContext设置为StackPanel,并设置按钮的Command和CommandParameter
<datagrid:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal"
VerticalAlignment="Center" DataContext="{StaticResource XDataContext}">
<Button x:Name="button" Content="View" Margin="5" Command="{Binding ViewOrganizationCommand}" CommandParameter="{Binding ElementName=dtgOrganizations, Path=SelectedItem}" >
</Button>
</StackPanel>
</DataTemplate>
</datagrid:DataGridTemplateColumn.CellTemplate>
DataGrid的每一行都将其DataContext设置为该对象。如果网格的ItemSource是ObservableCollection,则每行的DataContext就是组织对象
有两种方法可以解决这个问题
创建公开命令的换行视图或扩展视图模型。然后命令应该启动。下面是一些psuedo代码
public class OrganizationExtensionViewModel
{
<summary>
/// Private currentOrginization property.
/// </summary>
private Organization currentOrginization;
public Organization CurrentOrganization
{
get
{
return this.currentOrginization;
}
set
{
if (this.currentOrginization != value)
{
this.currentOrginization = value;
this.RaisePropertyChanged("CurrentOrganization");
}
}
}
public ViewModelCommand ViewOrganizationCommand { get; set; }
public OrganizationExtensionViewModel(Organization o)
{
this.CurrentOrganization = o;
this.ViewOrganizationCommand = new ViewModelCommand(this.ViewOrgnaizationClicked);
}
}
公共类组织扩展视图模型
{
///私有CurrentOrganization属性。
///
私人组织化;
公共组织
{
得到
{
返回此.CurrentOrganization;
}
设置
{
if(this.currentOrganization!=值)
{
此.currentOrganization=值;
本.RaiseProperty变更(“当前组织”);
}
}
}
公共ViewModelCommand ViewOrganizationCommand{get;set;}
公共组织扩展视图模型(组织o)
{
此.CurrentOrganization=o;
this.ViewOrganizationCommand=新建ViewModelCommand(单击this.ViewOrganizationCommand);
}
}
在xaml中将ViewModel定义为StaticResource,并将其称为绑定路径
。。。
在网格中
<Button x:Name="button" Content="View" Margin="5" Command="{Binding ViewOrganizationCommand, Source={StaticResource viewModel}}" />
我通过在中使用EventToCommand行为解决了这个问题,该行为带来了DataContextProxy的解决方案。应用此解决方案可以编写一个按钮代码,如Austin Lamb的答案。查看xaml和VM代码会很有帮助。请添加它。源代码按照您的建议添加:)您是否有任何绑定异常?您可以在输出窗口中看到它。