2016-04-20 19 views
2

xamDataGridのフィールドチューザーで検索機能を使用するために、フィールドチューザーとカスタムコードを使用しています。 テキストボックスを使用して検索し、リストボックスを使用してフィールド選択ツールのフィールドを維持しました。我々が使用している コード:私はこの問題を知っているWPFリストボックス内のスクロールバーの問題

<StackPanel> 
    <TextBox TextChanged="TextBox_TextChanged" /> 
     <ListBox x:Name="PART_FieldsListBox" ItemsSource="{TemplateBinding CurrentFields}" SelectionMode="Single" SelectedItem="{Binding Path=SelectedField, RelativeSource={x:Static RelativeSource.TemplatedParent}, Mode=TwoWay}" HorizontalContentAlignment="Stretch"> 
       <ListBox.ItemContainerStyle> 
         <Style TargetType="{x:Type ListBoxItem}"> 
           <Setter Property="FocusVisualStyle" Value="{x:Null}" /> 
            <Setter Property="Template"> 
            <Setter.Value> 
             <ControlTemplate TargetType="{x:Type ListBoxItem}"> 
             <Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="true"> 
              <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" /> 
              </Border> 
              </ControlTemplate> 
            </Setter.Value> 
            </Setter> 
          </Style> 
         </ListBox.ItemContainerStyle> 
      <ListBox.InputBindings> 
      <KeyBinding Key="Space" Command="{x:Static igDP:FieldChooserCommands.ToggleVisibility}"/> 
      </ListBox.InputBindings> 
      </ListBox> 

は、スタックパネルです。しかし、テキスト検索コードのため、スタックパネルを削除することはできません。検索のための コードは、我々が使用:我々はまた、スタックパネルの代わりにグリッドを交換した

private void TextBox_TextChanged(object sender, TextChangedEventArgs e) 
    { 
      ListBox lb = (((sender as TextBox).Parent as StackPanel).Children[1] as ListBox); 
      foreach (var item in lb.ItemsSource as ReadOnlyObservableCollection<FieldChooserEntry>) 
      { 
       ListBoxItem listBoxItem = (lb.ItemContainerGenerator.ContainerFromItem(item) as ListBoxItem); 
       if ((sender as TextBox).Text.ToString() != "") 
       { 
        if (item.Field.Label.ToString().ToLower().Contains((sender as TextBox).Text.ToLower())) 
         listBoxItem.Visibility = Visibility.Visible; 
        else 
         listBoxItem.Visibility = Visibility.Collapsed; 
       } 
       else 
        listBoxItem.Visibility = Visibility.Visible; 
      } 

    } 

。しかし、グリッドを追加した後に、テキスト変更イベントが発生しました。 "オブジェクト参照エラー"

ここで説明した問題の理由は、使用している検索TextBoxのTextChangedイベントに含まれています。 ListBox内のすべての項目が現在視覚化されていないため、ContainerFromItemメソッドがすべてをビジュアル要素として見つけることができないため、null参照例外がスローされます。 StackPanelをGridに置き換えて以来。代わりにforeachlb.ItemSourceを使用しての

、あなたがListBox.Itemsを使用することができ、その後、あなただけのアイテムをキャストする必要があります:あなたのFieldChooserEntryおよび他の要素のために利用可能なコードがないので、私は次のことをお勧めしたい

+0

StackPanelをグリッドに置き換えた場合。 StackPanel'''を '' '.Parent''を' 'Grid'''に変更しましたか? –

+0

はい、私はグリッドとして.Parentを変更しました。 –

答えて

0

ifFieldChooserEntryと表示されますか?

この方法では、ItemContainerGeneratorの複雑さを避けることができます。これは、アイテムを取得する最も良い方法ではない場合があります。仮想化に問題が生じる可能性があります。

グリッドに関するエラーについては、Children[1]が実際にリストボックスであるかどうかを確認したい場合があります。 GridとStackPanelのコントロールの順序は異なる場合があります。

詳細が必要な場合や、問題が解決しない場合は、FieldChooserEntryオブジェクトのコード(少なくとも構造体)を投稿する必要があります。

+0

以下のコードでキャストした後、リストボックスの項目が表示されません。 ListBox lb =((TextBoxとしての送信者).ParentをGridとして).Children [1] as ListBox); foreach(lb.Items内のvarアイテム) { ListBoxItem listBoxItem =アイテムをListBoxItemとして返します。 } –

+0

私は '' 'ListBox lb = ...' ''行にブレークポイントを設定し、どのリストがListBoxであるかを見るためにChildrenのリストを見ることをお勧めします。 –

+0

lb.ItemsはFieldChooserEntryになります。 ListBox Itemにキャストしている間、ListBoxItemはnullです。 FieldChooserEntryをListBoxItemに直接キャストできないためです。 –

関連する問題