在代码中绑定到self

时间:2016-06-06 13:40:08

标签: xamarin.forms

我在代码中定义了一个DataTemplate,我想设置绑定到它自己。

DataTemplate = new DataTemplate(typeof(MyCustomViewCell));
DataTemplate.SetBinding(MyCustomViewCell.MyCustomProperty, "");

上面的代码不起作用。您不能在该方法中使用空路径。我通常使用的XAML等价物是MyCustomProperty="{Binding}"。这会将属性设置为当前BindingContext / DataContext

在C#中实现此目的的语法是什么?

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:0)

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