【发布时间】:2018-01-10 17:57:07
【问题描述】:
我需要在 ListView 中动态使用 DataTemplate。此数据模板是一个用户控件。我可以动态调用用户控件。但我无法从用户控件中读取项目。
<ListView.ItemTemplate>
<DataTemplate xmlns:local ="using:App4.Components" x:DataType="models:modelAuftrag">
<local:ucPosListeConteiner Test="{x:Bind auftragNummer}"/>
</DataTemplate>
</ListView.ItemTemplate>
我分享一个代码作为例子。
public static readonly DependencyProperty TestProperty = DependencyProperty.Register
(
"Test",
typeof(string),
typeof(ucPosListeConteiner),
new PropertyMetadata("")
);
public string Test
{
get { return (string)GetValue(TestProperty); }
set { SetValue(TestProperty, value);}
}
和构造函数;
viewModelUcPosListeConteiner model;
public ucPosListeConteiner()
{
this.InitializeComponent();
model = new viewModelUcPosListeConteiner();
this.DataContext = this;
}
运行时;
如果我在构造函数中删除 this.DataContext = this 语句,代码不会出错。但这次绑定在用户控制下不起作用。
如何在 UserControl 中获取传出数据和绑定?
谢谢...
【问题讨论】:
标签: c# dynamic data-binding user-controls datatemplate