Xamarin.forms 在代码中绑定到self

Xamarin.forms 在代码中绑定到self,xamarin.forms,Xamarin.forms,我正在用代码定义一个DataTemplate,我想将绑定设置为自身 DataTemplate = new DataTemplate(typeof(MyCustomViewCell)); DataTemplate.SetBinding(MyCustomViewCell.MyCustomProperty, ""); 上面的代码不起作用。在该方法中不能使用空路径。我通常使用的XAML等价物是MyCustomProperty=“{Binding}”。这将属性设置为当前的BindingContext/D

我正在用代码定义一个DataTemplate,我想将绑定设置为自身

DataTemplate = new DataTemplate(typeof(MyCustomViewCell));
DataTemplate.SetBinding(MyCustomViewCell.MyCustomProperty, "");
上面的代码不起作用。在该方法中不能使用空路径。我通常使用的XAML等价物是
MyCustomProperty=“{Binding}”
。这将属性设置为当前的
BindingContext
/
DataContext

在C#中实现这一点的语法是什么?

DataTemplate.SetBinding(MyCustomViewCell.MyCustomProperty,x=>x.PropertyOfYourType);

使用“.”作为绑定路径来引用BindingContext本身。

我一直使用“.”作为绑定路径来直接引用context@Jason如果您将您的评论作为答案发布,我会将其标记为解决方案。完成,很高兴它对您有效。如果不指定类型,则此操作不起作用,但@Jason的评论似乎是一个更好的解决方案。
DataTemplate.SetBinding<YourType>(MyCustomViewCell.MyCustomProperty, x => x.PropertyOfYourType);