[英]bind listbox to selected item
WP8,VS2012 ...我正在使用msdn本地数据库示例作为基础。
这是我当前的设置,那里是75%...
我有MainPage枢纽>列表框AllItems,它显示数据库中的所有当前项目:
XAML
<ListBox
SelectionChanged="OpenWinePage_Click"
x:Name="allItemsListBox"
ItemsSource="{Binding AllItems}"
Margin="12,2,-20,-2" Width="440"
ItemTemplate="{StaticResource WineListBoxItemTemplate}" />
我希望能够单击其中一项,即Mark Ryan,并且只有该项显示在“详细信息”页面中:
代码OpenWinePage_Click的背后是:
private void OpenWinePage_Click(object sender, EventArgs e)
{
// Capture selected item data
_selectedItem = (sender as ListBox).SelectedItem;
if (_selectedItem != null)
{
// Send ID of selected contact
string dest = "/WinePage.xaml?toDoItemId=" + ((ToDoItem)_selectedItem).ToDoItemId;
NavigationService.Navigate(new Uri(dest, UriKind.Relative));
}
}
打开WinePage.xaml时,显示所选项目的xaml是:
<ListBox
x:Name="WinePageDetails"
Margin="12, 0, 12, 0" Width="440"
ItemsSource="{Binding AllItems}" <!--I DON'T KNOW WHAT TO BIND HERE TO ONLY SHOW SELECTED ITEM-->
ItemTemplate="{StaticResource WinePageListBoxItemTemplate}" />
因此,我在详细信息页面中获得了所有项目,而不仅是所选的1个项目:
在我的ToDoViewModel.cs中,我尝试绑定以下内容?...
// All items.
private ObservableCollection<ToDoItem> _allItems;
public ObservableCollection<ToDoItem> AllItems
{
get { return _allItems; }
set
{
_allItems = value;
NotifyPropertyChanged("AllItems");
}
}
// To-do items associated with the red category.
private ObservableCollection<ToDoItem> _redItems;
public ObservableCollection<ToDoItem> RedItems
{
get { return _redItems; }
set
{
_redItems = value;
NotifyPropertyChanged("RedItems");
}
}...et al pivots
我是否需要创建另一个仅指向一个项目的可观察集合,以便可以对其进行绑定?
这是一个有约束力的问题吗? 数据库问题? 如何仅将一个选定的项目显示在我的详细信息页面上?
非常感谢您的帮助!
[R
您需要创建一个NotifyPropertyChanged属性,例如,在OpenWinePage_Click
时设置的Public TodoItem SelectedTodo {...}
。 然后,您可以简单地将该特定属性绑定到控件:例如<TextBlock Text="{Binding SelectedTodo}"/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.