由于标题声明我在xaml中的ListView绑定有效,但在c#中不起作用。
以下是代码段: XAML
<ListView ItemsSource="{Binding Records}">
<ListView.ItemTemplate>
<DataTemplate>
<TextCell Text="{Binding}">
</TextCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
C#
ListView lv = new ListView();
lv.ItemsSource = ClassVMInstance.Records;
var dt = new DataTemplate(typeof(TextCell));
dt.SetBinding(TextCell.TextProperty, new Binding("Records"));
lv.ItemTemplate = dt;
ClassVMInstance 是我的ViewModel的一个实例。
记录是ObservableCollection<string>
xaml版本工作正常,它显示文本,但c#版本只有没有文本的空列表元素。
(我在同一页面上用2个listview测试了一个xaml和另一个c#,只有xaml显示了文本,但c#版本只有相同数量的列表项但是空的)
我相信itemssource属性在代码中正常工作但绑定不是有人可以帮助我。
答案 0 :(得分:2)
试试这个
dt.SetBinding(TextCell.TextProperty, new Binding("."));