Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将当前项作为数据模板中的命令参数发送_C#_Wpf_Xaml_Mvvm_Binding - Fatal编程技术网

C# 将当前项作为数据模板中的命令参数发送

C# 将当前项作为数据模板中的命令参数发送,c#,wpf,xaml,mvvm,binding,C#,Wpf,Xaml,Mvvm,Binding,考虑以下类别: public class Customer { public Int32 Id { get; set; } public String FirstName { get; set; } public String LastName { get; set; } } 考虑以下视图模型(在MVVM设计模式中) 试试这个: <Button Content="DELETE!" Command="{Binding ElementName=C

考虑以下类别:

public class Customer
{
    public Int32 Id { get; set; }
    public String FirstName { get; set; }
    public String LastName { get; set; }
}
考虑以下视图模型(在MVVM设计模式中)

试试这个:

 <Button Content="DELETE!" 
         Command="{Binding ElementName=CustomersManagement, Path=DataContext.RemoveCustomer}" 
         CommandParameter="{Binding}"/>

请参考以下示例

<Window x:Class="DelegateComd_Learning.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525" x:Name="CustomersManagement" >
<Window.Resources>       

</Window.Resources>
<StackPanel>
    <ItemsControl ItemsSource="{Binding Persons}">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal" x:Name="stck">
                    <Label Content="{Binding FirstName}" />
                    <Label Content="{Binding LastName }" />
                    <Button Content="DELETE!" DataContext="{Binding ElementName=CustomersManagement , Path=DataContext }" Command="{Binding RemoveCustomer }"
                            CommandParameter="{Binding ElementName=stck,Path=DataContext}"/>
                </StackPanel>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <WrapPanel />
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
    </ItemsControl>
</StackPanel>

公共部分类主窗口:窗口
{
公共主窗口()
{
初始化组件();
this.DataContext=newtestviewmodel();
}      
}
类TestViewModel
{
私有可观测集合myVar;
公众人士
{
获取{return myVar;}
设置{myVar=value;}
}
public DelegateCommand RemoveCustomer{get;set;}
公共TestViewModel()
{
人员=新的可观察集合();
对于(int i=0;i<10;i++)
{
添加(newperson(){FirstName=“Test”+i,LastName=“Testlst”+i});
}
RemoveCustomer=新的DelegateCommand(Removeperson);
}
私人无效删除人(个人prps)
{
}
}
公共阶层人士
{
私有字符串名;
公共字符串名
{
获取{return firstName;}
设置{firstName=value;}
}
私有字符串lastName;
公共字符串姓氏
{
获取{return lastName;}
设置{lastName=value;}
}
}

@Will您能提供更多详细信息吗?绑定到整个DataContext而不是DataTemplate绑定到的对象。@rolls绑定有一个默认路径,值为“.”,没问题。
DataContext="{Binding ElementName=CustomersManagement, Path=DataContext }" 
    Command="{Binding RemoveCustomer }" CommandParamter="??????????"
 <Button Content="DELETE!" 
         Command="{Binding ElementName=CustomersManagement, Path=DataContext.RemoveCustomer}" 
         CommandParameter="{Binding}"/>
<Window x:Class="DelegateComd_Learning.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525" x:Name="CustomersManagement" >
<Window.Resources>       

</Window.Resources>
<StackPanel>
    <ItemsControl ItemsSource="{Binding Persons}">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal" x:Name="stck">
                    <Label Content="{Binding FirstName}" />
                    <Label Content="{Binding LastName }" />
                    <Button Content="DELETE!" DataContext="{Binding ElementName=CustomersManagement , Path=DataContext }" Command="{Binding RemoveCustomer }"
                            CommandParameter="{Binding ElementName=stck,Path=DataContext}"/>
                </StackPanel>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <WrapPanel />
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
    </ItemsControl>
</StackPanel>
 public partial class MainWindow : Window
{

    public MainWindow()
    {
        InitializeComponent();
        this.DataContext = new TestViewModel();
    }      
}
class TestViewModel
{
    private ObservableCollection<Person> myVar;

    public ObservableCollection<Person> Persons
    {
        get { return myVar; }
        set { myVar = value; }
    }

    public DelegateCommand<Person> RemoveCustomer { get; set; }

    public TestViewModel()
    {
        Persons = new ObservableCollection<Person>();
        for (int i = 0; i < 10; i++)
        {
            Persons.Add(new Person() { FirstName = "Test"+i, LastName = "Testlst"+i });
        }
        RemoveCustomer = new DelegateCommand<Person>(Removeperson);
    }
    private void Removeperson(Person prps)
    {

    }
}

public class Person
{
    private string firstName;

    public string FirstName
    {
        get { return firstName; }
        set { firstName = value; }
    }
    private string lastName;

    public string LastName
    {
        get { return lastName; }
        set { lastName = value; }
    }
}