C# 是否访问windows phone 7中列表框中的项目?
在我的代码中C# 是否访问windows phone 7中列表框中的项目?,c#,windows-phone-7,listbox,C#,Windows Phone 7,Listbox,在我的代码中 protected override void OnNavigatedTo(NavigationEventArgs e) { using (bus_noContext ctx = new bus_noContext(bus_noContext.ConnectionString)) { ctx.CreateIfNotExists(); ctx.LogDebug = true;
protected override void OnNavigatedTo(NavigationEventArgs e)
{
using (bus_noContext ctx = new bus_noContext(bus_noContext.ConnectionString))
{
ctx.CreateIfNotExists();
ctx.LogDebug = true;
var buses = from c in ctx.Bus_routes
select new bus_list{ BUS_NO = c.BUS_NO, SOURCE = c.SOURCE, DESTINATION = c.DESTINATION};
busno_list.ItemsSource = buses.ToList();
}
}
private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
{
string temp;
TextBlock nameBox;
ListBoxItem currentSelectedListBoxItem;
for(int i=0;i<busno_list.Items.Count;i++)
{
currentSelectedListBoxItem = this.busno_list.ItemContainerGenerator.ContainerFromIndex(i) as ListBoxItem;
nameBox = FindDescendant<TextBlock>(currentSelectedListBoxItem);
temp = nameBox.Text;
if (temp.Contains(searchbox.Text))
{
busno_list.SelectedIndex = i;
busno_list.ScrollIntoView(busno_list.SelectedItem);
return;
}
}
}
受保护的覆盖无效OnNavigatedTo(NavigationEventArgs e)
{
使用(bus_noContext ctx=新的bus_noContext(bus_noContext.ConnectionString))
{
ctx.CreateIfNotExists();
ctx.LogDebug=true;
var总线=从ctx.Bus\U路线中的c开始
选择新总线列表{bus_NO=c.bus_NO,SOURCE=c.SOURCE,DESTINATION=c.DESTINATION};
busno_list.ItemsSource=bus.ToList();
}
}
私有void TextBox\u TextChanged(对象发送者,textchangedventargs e)
{
字符串温度;
文本块名称框;
ListBoxItem currentSelectedListBoxItem;
对于(int i=0;i当您拥有源时,我不确定您为什么要通过查找生成的元素来执行此操作,例如:
protected override void OnNavigatedTo(NavigationEventArgs e)
{
using (bus_noContext ctx = new bus_noContext(bus_noContext.ConnectionString))
{
ctx.CreateIfNotExists();
ctx.LogDebug = true;
var buses = from c in ctx.Bus_routes
select new bus_list{ BUS_NO = c.BUS_NO, SOURCE = c.SOURCE, DESTINATION = c.DESTINATION};
busno_list.ItemsSource = buses.ToList();
searchbox.Text = buses[20].SOURCE; // or whatever
}
}
先生,我的动机不是访问特定的记录,但我在这里试图实现的是,每当用户在searchbox中输入子字符串时,它都应该滚动到所需的行。我正在编辑问题,请再看一眼。通过采用MVVM模式,您可能会发现这更容易做到——即,有一个可重复的类在搜索屏幕(即ViewModel)后面发送数据,其中包含适用的公交路线集合、搜索词属性和所选索引属性-您应该能够在XAML中轻松绑定它们,而无需在视图后面的代码中对此进行破解
protected override void OnNavigatedTo(NavigationEventArgs e)
{
using (bus_noContext ctx = new bus_noContext(bus_noContext.ConnectionString))
{
ctx.CreateIfNotExists();
ctx.LogDebug = true;
var buses = from c in ctx.Bus_routes
select new bus_list{ BUS_NO = c.BUS_NO, SOURCE = c.SOURCE, DESTINATION = c.DESTINATION};
busno_list.ItemsSource = buses.ToList();
searchbox.Text = buses[20].SOURCE; // or whatever
}
}