C# 将命令绑定到ItemTemplate内的按钮

C# 将命令绑定到ItemTemplate内的按钮,c#,command,windows-mobile,xaml-binding,C#,Command,Windows Mobile,Xaml Binding,您好,我正在尝试将命令绑定到LongListSelector的ItemTemplate中的按钮。但面临约束问题。这是我的密码- XAML <DataTemplate x:Key="ItemTemplate"> <StackPanel Height="108" Width="308" Margin="6,6"> <TextBlock Text="{Binding Name}" Foreground="red"></T

您好,我正在尝试将命令绑定到LongListSelector的ItemTemplate中的按钮。但面临约束问题。这是我的密码-

XAML

<DataTemplate x:Key="ItemTemplate">
        <StackPanel Height="108" Width="308" Margin="6,6">
            <TextBlock Text="{Binding Name}" Foreground="red"></TextBlock>
            <TextBlock Text="{Binding Type}" Foreground="red"></TextBlock>
            <Button Content="add to emergency" Foreground="White" Background="red" Command="{Binding Path=DataContext.MyViewModelCommand}"/>
        </StackPanel>
    </DataTemplate>
视图模型

public class HspitalVM:INotifyPropertyChanged
{
    public HspitalVM()
    {
        MyViewModelCommand = new ActionCommand(DoSomething);
    }

    public ICommand MyViewModelCommand { get; private set; }
    private void DoSomething()
    {

    }
}
<Button x:Name="button" Content="add to emergency" Foreground="White"  Background="#FFF7203C" Command="{Binding HspitalVM.GiveDetails, Mode=OneWay}" DataContext="{Binding Source={StaticResource Locator}}" CommandParameter="{Binding DataContext, RelativeSource={RelativeSource TemplatedParent}}"/>
public RelayCommand<object> GiveDetails { get; private set; }

public HspitalVM()
    {
        hspitalList=ReadToObject();
        GiveDetails = new RelayCommand<object>(DoSomething);
    }

 private void DoSomething(object param)
    {
        var fetchedCountry = (Hspital)param;

        MessageBox.Show("The country name is " + fetchedCountry.Name);
    }

对于裸按钮,该命令可以正常工作,但在ItemTemplate中不起作用。请指导我

使用Windows Phone的最佳解决方案是使用mvvmlight toolkit,并使用viewmodellocator类作为源,并通过该类绑定正确的命令,在这种情况下,如果需要,您可以使用事件到命令行为

您可以查看更多详细信息

如果你不想使用mvvmlight工具包,你可以看看这个博客,它展示了用FindAncestor模式绑定RelativeSource绑定的方法


希望有帮助。:)

主要是因为按钮绑定到项目
DataContext
和 不是ViewModel DataContext,您应该修改命令绑定 要进行如下操作:

Command="{Binding Path=DataContext.MyViewModelCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}
x:Type应该是ViewModel
DataContext
设置为,它可以是
页面
用户控件
或任何容器,基本上是包含ViewModel
数据上下文的控件

您的命令必须有一个参数,因为您将对 项目本身,如下所示

 CommandParameter="{Binding}"
这意味着命令参数是在视图中单击按钮的项目,您可以在ViewModel中通过将其引用添加到另一个列表或删除它来对其作出反应,具体取决于您要执行的操作

答案终于出来了

XAML(用于页面)


XAML(用于控制)


视图模型

public class HspitalVM:INotifyPropertyChanged
{
    public HspitalVM()
    {
        MyViewModelCommand = new ActionCommand(DoSomething);
    }

    public ICommand MyViewModelCommand { get; private set; }
    private void DoSomething()
    {

    }
}
<Button x:Name="button" Content="add to emergency" Foreground="White"  Background="#FFF7203C" Command="{Binding HspitalVM.GiveDetails, Mode=OneWay}" DataContext="{Binding Source={StaticResource Locator}}" CommandParameter="{Binding DataContext, RelativeSource={RelativeSource TemplatedParent}}"/>
public RelayCommand<object> GiveDetails { get; private set; }

public HspitalVM()
    {
        hspitalList=ReadToObject();
        GiveDetails = new RelayCommand<object>(DoSomething);
    }

 private void DoSomething(object param)
    {
        var fetchedCountry = (Hspital)param;

        MessageBox.Show("The country name is " + fetchedCountry.Name);
    }
public RelayCommand GiveDetails{get;private set;}
公共HspitalVM()
{
hspitalist=ReadToObject();
GiveDetails=新继电器命令(DoSomething);
}
私有void DoSomething(对象参数)
{
var fetchedCountry=(Hspital)参数;
MessageBox.Show(“国家名称为”+fetchedCountry.name);
}

我遇到了类似的问题,我尝试了此解决方案,并确认其有效。谢谢