如何在代码中执行与 {Binding Source} 等效的操作?
How to do the equivalent of {Binding Source} in code?
我正在扩展一个控件,以便能够在我当前的 Xamarin 项目中重用它。作为此控件的一部分,我需要以编程方式创建一个 DataTemplate。我已经弄清楚了这部分并且它工作正常。
DataTemplate 中有一个标签。我需要将 Label 的 BindingContext 属性 绑定到 {Binding Source}
。我需要将标签的文本 属性 绑定到 {Binding Path=Name}
.
这在 XAML 中有效,但我不想将它复制到代码库中的一百万个不同位置。
<dxGrid:TemplateColumn FieldName="MyPropertyName"
Caption="MyColumn">
<dxGrid:TemplateColumn.DisplayTemplate>
<DataTemplate>
<Label BindingContext="{Binding Source}"
Text="{Binding Source, Path=MyPropertyName}" />
</DataTemplate>
</dxGrid:TemplateColumn.DisplayTemplate>
我的扩展控件现在看起来像这样:
public class MyColumn : TemplateColumn
{
public MyColumn()
{
DataTemplate displayTemplate = new DataTemplate(() =>
{
BindingBase textBinding = new Binding(FieldName);
Label label = new Label();
// TODO: Bind BindingContextProperty to {Binding Source}
//label.SetBinding(BindingContextProperty, binding);
label.SetBinding(Label.TextProperty, textBinding);
return new ViewCell
{
View = label
};
});
DisplayTemplate = displayTemplate;
}
}
我被绑定挂断了,因为我不确定如何在代码中执行与 {Binding Source}
等效的操作。任何帮助将不胜感激。
很简单,用属性
的名字
label.SetBinding(BindingContextProperty, "Source");
@Eugene - 感谢您的回复。不幸的是,这不起作用并且绑定到 "Source" 会引发 Null Reference Exception。今天早上我又通过了一次,并以这种方式工作:
public MyColumn()
{
DataTemplate displayTemplate = new DataTemplate(() =>
{
Grid grid = new Grid();
grid.SetBinding(Grid.BindingContextProperty, "Source");
Label label = new Label();
label.SetBinding(Label.TextProperty,FieldName);
grid.Children.Add(label);
return grid;
});
this.DisplayTemplate = displayTemplate;
}
我正在扩展一个控件,以便能够在我当前的 Xamarin 项目中重用它。作为此控件的一部分,我需要以编程方式创建一个 DataTemplate。我已经弄清楚了这部分并且它工作正常。
DataTemplate 中有一个标签。我需要将 Label 的 BindingContext 属性 绑定到 {Binding Source}
。我需要将标签的文本 属性 绑定到 {Binding Path=Name}
.
这在 XAML 中有效,但我不想将它复制到代码库中的一百万个不同位置。
<dxGrid:TemplateColumn FieldName="MyPropertyName"
Caption="MyColumn">
<dxGrid:TemplateColumn.DisplayTemplate>
<DataTemplate>
<Label BindingContext="{Binding Source}"
Text="{Binding Source, Path=MyPropertyName}" />
</DataTemplate>
</dxGrid:TemplateColumn.DisplayTemplate>
我的扩展控件现在看起来像这样:
public class MyColumn : TemplateColumn
{
public MyColumn()
{
DataTemplate displayTemplate = new DataTemplate(() =>
{
BindingBase textBinding = new Binding(FieldName);
Label label = new Label();
// TODO: Bind BindingContextProperty to {Binding Source}
//label.SetBinding(BindingContextProperty, binding);
label.SetBinding(Label.TextProperty, textBinding);
return new ViewCell
{
View = label
};
});
DisplayTemplate = displayTemplate;
}
}
我被绑定挂断了,因为我不确定如何在代码中执行与 {Binding Source}
等效的操作。任何帮助将不胜感激。
很简单,用属性
的名字label.SetBinding(BindingContextProperty, "Source");
@Eugene - 感谢您的回复。不幸的是,这不起作用并且绑定到 "Source" 会引发 Null Reference Exception。今天早上我又通过了一次,并以这种方式工作:
public MyColumn()
{
DataTemplate displayTemplate = new DataTemplate(() =>
{
Grid grid = new Grid();
grid.SetBinding(Grid.BindingContextProperty, "Source");
Label label = new Label();
label.SetBinding(Label.TextProperty,FieldName);
grid.Children.Add(label);
return grid;
});
this.DisplayTemplate = displayTemplate;
}