[英]Xamarin.Forms and binding for ElementName
I'm trying to bind my ListView Item to Command from my parent ViewModel. 我正在尝试将我的ListView项目从父ViewModel绑定到Command。 The problem is that I would like to add CommandParameter with current Item 问题是我想在当前项目中添加CommandParameter
Basically, in WPF i would do something like 基本上,在WPF中,我会做类似的事情
<MyItem Command="{Binding ElementName=parent", Path=DataContext.MyCommand}" CommandParameter="{Binding}"/>
In Xamarin Forms ElementName is not working, so the way is to use BindingContext, but how I should use it (if one of my binding points to parent, second one to self)? 在Xamarin Forms中,ElementName不起作用,因此方法是使用BindingContext,但是我应该如何使用它(如果我的绑定点之一指向父级,第二个绑定点指向self)?
I have tried 我努力了
<MyItem Command="{Binding BindingContext.RemoveCommand, Source={x:Reference parent}}" CommandParameter="{Binding }" />
But it does not work (seems it not changing Source). 但这是行不通的(似乎没有更改Source)。
I know, that with normal binding a way is to use BindingContext="{x:Reference parent}"
, but it won't work in this example, because I need Self
binding for CommandParameter 我知道,普通绑定的一种方法是使用BindingContext="{x:Reference parent}"
,但是在此示例中将不起作用,因为我需要对CommandParameter进行Self
绑定
How can i do it? 我该怎么做?
I understand you want to execute a command on the parent node of your current node but pass the current node as a parameter. 我了解您想在当前节点的父节点上执行命令,但是将当前节点作为参数传递。 If that is the case, you can solve it like this: 如果是这样,您可以这样解决:
Here's the model we bind to. 这是我们绑定的模型。 It has a Parent
property and it defines an ICommand
(please note that all is C#6 code, so you'll need XS or VS2015!): 它具有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; }
}
In the code behind, the binding context is set: 在后面的代码中,设置了绑定上下文:
this.BindingContext = new BindingClass ("Bound Button") {
Parent = new BindingClass ("Parent")
};
And in the XAML, we have a button that calls the command of the parent node and passes the current node as a parameter: 在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.