如何使列表视图与数据绑定一起使用? [英] How do I make List View work with data binding?
问题描述
伙计们
我已经设法在Xaml中进行数据绑定但不完全。
我有文本块和数据绑定工作。我在Windows应用程序中将信息从第2页传递到第3页。
我的列表视图完全相同,但根本不起作用。我不明白为什么。有人可以看看吗?
Hi, guys
I've managed to do Data Binding in Xaml but not completely.
I have got text block and Data Binding works. I'm passing information from page 2 to page3 in my windows app.
I have done exactly the same with my List View and it doesn't work at all. I don't understand why. Can someone please have a look?
<ListView Name="detailsListView" Grid.Row="1" Height="300" Width="400"
ItemsSource="{Binding Name}"/>
<TextBlock Text="{Binding Name}" x:Name="myTextBlock" Grid.Row="1" Grid.Column="2" Height="200" Width="300" FontSize="28"/>
在我的代码背后数据上下文设置为页面布局
In my Code Behind The data context is set to the page layout
protected override void OnNavigatedTo(NavigationEventArgs e)
{
Nutrient nutriQuery = e.Parameter as Nutrient;
if (nutriQuery != null)
{
DetailsPageLayout.DataContext = nutriQuery;
}
为什么文本块可以工作而不是列表视图?
Why Text Block works but not List View?
推荐答案
您需要将ItemsSource绑定到ObservableCollection。您还需要定义ListView视图。这样的事情。
You need to bind the ItemsSource to an ObservableCollection. You also need to define the ListView View. Something like this.
<!--In the view model, PersonCollection is a collection of type Person
SelectedPerson is a property of type Person.Name and Age are properties defined inside the Person class-->
<listview itemssource="{Binding PersonCollection}" selecteditem="{Binding SelectedPerson, Mode=TwoWay}" issynchronizedwithcurrentitem="True">
HorizontalAlignment="Center" Height="168" VerticalAlignment="Top" Width="400" Background="#FFBFBFBF">
<listview.view>
<gridview>
<gridviewcolumn width="280" header="Name" displaymemberbinding="{Binding Path=Name}" />
<gridviewcolumn width="60" header="Age" displaymemberbinding="{Binding Path=Age}" />
</gridview>
</listview.view>
</listview>
这篇关于如何使列表视图与数据绑定一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!