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に置き換えて以来。代わりにforeach
でlb.ItemSource
を使用しての
、あなたがListBox.Items
を使用することができ、その後、あなただけのアイテムをキャストする必要があります:あなたのFieldChooserEntryおよび他の要素のために利用可能なコードがないので、私は次のことをお勧めしたい
StackPanelをグリッドに置き換えた場合。 StackPanel'''を '' '.Parent''を' 'Grid'''に変更しましたか? –
はい、私はグリッドとして.Parentを変更しました。 –