0

Windows Phone 7 のサーチャーに似たようなことをしようとしています。私が行ったことは次のとおりです。TextChanged イベントを持つ TextBox と、HyperlinkBut​​tons の Listbox があります。私が試したことはこれです:

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 を返すだけです...

何か案が?

皆さん、ありがとうございました

4

2 に答える 2

0

ポイントがリストボックス内の要素をフィルタリングすることである場合は、 CollectionViewSourceを使用します。

System.Windows.Data.CollectionViewSource cvs;
private void SetSource(IEnumerable<string> source)
{
     cvs = new System.Windows.Data.CollectionViewSource();
     cvs.Source=source;
     listBox1.ItemsSource = cvs.View;
}
private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
{
     var box = (TextBox)sender;
     if (!string.IsNullOrEmpty(box.Text))
        cvs.View.Filter = o => ((string)o).Contains(box.Text);
     else
        cvs.View.Filter = null;
}

ItemContainer を使用する際の問題は、アイテムを表示する必要がある場合にのみ作成されることです。そのため、null 値があります。

于 2011-07-27T15:43:16.633 に答える
0

Color Data Binding を使用しないのはなぜですか? もっと簡単な方法である必要があります。

<HyperlinkButton x:Name="{Binding id}" Content="{Binding fullName}" Background="{Binding BackgroundColor}" FontSize="24"    Click="NavigateToFriend_Click" />


private SolidColorBrush _backgroundColor;

public SolidColorBrush BackgroundColor{
    get {

        return _backgroundColor;
    }
    set { _backgroundColor= value; }
}

private void searchFriend_TextChanged(object sender, TextChangedEventArgs e)
{
 int index = 0;
 foreach (Person person in lbFriends.Items)
 {
   if (!person.fullName.Contains((sender as TextBox).Text))
   {
     person.BackgroundColor= new SolidColorBrush(Colors.Black);
   }
   index++;
  }
 }
于 2011-07-27T14:05:18.663 に答える