我正在嘗試在Windows Phone 7中執行類似於搜索器的操作,並且我所做的是以下操作,我有一個帶有TextChanged事件的TextBox和一個HyperlinkButtons的Listbox。我嘗試那就是:Silverlight搜索器
private void searchFriend_TextChanged(object sender, TextChangedEventArgs e)
{
int index = 0;
foreach (Person person in lbFriends.Items)
{
ListBoxItem lbi = lbFriends.ItemContainerGenerator.ContainerFromItem(index) as ListBoxItem;
lbi.Visibility = Visibility.Visible;
if (!person.fullName.Contains((sender as TextBox).Text))
{
lbi.Background = new SolidColorBrush(Colors.Black);
}
index++;
}
}
這裏是XAML:
<TextBox x:Name="searchFriend" TextChanged="searchFriend_TextChanged" />
<ListBox x:Name="lbFriends" Height="535" Margin="0,0,0,20">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<HyperlinkButton x:Name="{Binding id}" Content="{Binding fullName}" FontSize="24" Click="NavigateToFriend_Click" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
這裏的問題是,當我有68種以上的元素,在ContainerFromItem剛剛返回null ListBoxItems ...
有什麼想法?
謝謝大家
耶!那就是答案!謝謝! –