[英]Xamarin.Forms and binding for ElementName
我正在尝试将我的ListView项目从父ViewModel绑定到Command。 问题是我想在当前项目中添加CommandParameter
基本上,在WPF中,我会做类似的事情
<MyItem Command="{Binding ElementName=parent", Path=DataContext.MyCommand}" CommandParameter="{Binding}"/>
在Xamarin Forms中,ElementName不起作用,因此方法是使用BindingContext,但是我应该如何使用它(如果我的绑定点之一指向父级,第二个绑定点指向self)?
我努力了
<MyItem Command="{Binding BindingContext.RemoveCommand, Source={x:Reference parent}}" CommandParameter="{Binding }" />
但这是行不通的(似乎没有更改Source)。
我知道,普通绑定的一种方法是使用BindingContext="{x:Reference parent}"
,但是在此示例中将不起作用,因为我需要对CommandParameter进行Self
绑定
我该怎么做?
我了解您想在当前节点的父节点上执行命令,但是将当前节点作为参数传递。 如果是这样,您可以这样解决:
这是我们绑定的模型。 它具有Parent
属性,并定义了一个ICommand
(请注意,所有代码都是C#6代码,因此您需要XS或VS2015!):
public class BindingClass
{
public BindingClass (string title)
{
this.TestCommand = new TestCommandImpl (this);
this.Title = title;
}
public string Title { get; }
public ICommand TestCommand { get; }
public BindingClass Parent { get; set; }
}
在后面的代码中,设置了绑定上下文:
this.BindingContext = new BindingClass ("Bound Button") {
Parent = new BindingClass ("Parent")
};
在XAML中,我们有一个按钮,该按钮调用父节点的命令并将当前节点作为参数传递:
<Button
x:Name="btnTest"
Text="{Binding Title}"
Command="{Binding Parent.TestCommand}"
CommandParameter="{Binding}"
VerticalOptions="CenterAndExpand"
HorizontalOptions="CenterAndExpand"/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.