简体   繁体   中英

Collection is not Filtering the List Items in WPF

I have a Observable Collection, I want to Filter the collection. The Collection class like a dictionary. I have tried the following code But it is not filtering the collection. I know it is very basic question. I am new to c#. I am studying. Please any one help me to filter the collection. I have mentioned the code below, please refer

GroupModel

public class GroupModel
{
    public string Key { get; set; }
    public ObservableCollection<ValueModel> Values { get; set; }
}

ValueModel

public class ValueModel
{
    public int Id { get; set; }
    public string Name { get; set; } 
}

ViewModel

Public Class ViewModel
{
     public ObservableCollection<GroupModel> MainValues
     {
         get { return mainValues; }
         set { mainValues = value; }
     }

     public string SearchIndicator
     {
         get { return searchIndicator; }
         set
         {
            searchIndicator = value;
            OnPropertyChanged(SearchIndicator);                
            ItemsView.Refresh();
         }
     }

    public ViewModel(IMainValueCollection mainValueCollection)
    {
       MainValue = new ObservableCollection<GroupModel>();
       this.MainValue = mainValueCollection;
       ItemsView.Filter = new Predicate<object>(o => Filter(o as ValueModel));
    }

    public ICollectionView ItemsView
    {            
       get
       {
          var value = (MainValues.SelectMany(a => a.Values)).ToList();
          return CollectionViewSource.GetDefaultView((MainValues.SelectMany(a => a.Values)));
       }
    }
    private bool Filter(ValueModel value)
    {
       if (SearchIndicator == null)
       {
          return true;
       }
       return value.Name.IndexOf(SearchIndicator, StringComparison.OrdinalIgnoreCase) != -1;
    }
}

MainViewUI

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>

    <Grid
        Grid.Row="0"
        Height="35"
        Margin="5,5,0,5">
        <Border
            Margin="10,10,10,0"
            Padding="1"
            BorderBrush="LightGray"
            BorderThickness="1"
            CornerRadius="0">
            <Grid
                Height="30"
                HorizontalAlignment="Stretch"
                VerticalAlignment="Center"
                Background="White">
                <Grid.RowDefinitions>
                    <RowDefinition Height="*" />
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>

                <Grid
                    Grid.Row="0"
                    Grid.Column="0"
                    Margin="2,0,0,0"
                    HorizontalAlignment="Center"
                    VerticalAlignment="Center">
                    <Image
                        Width="25"
                        Height="25"
                        HorizontalAlignment="Center"
                        VerticalAlignment="Center"
                        Source="/Images/SearchIcon.png" />
                </Grid>
                <Grid
                    Name="TextBlockGrid"
                    Grid.Row="0"
                    Grid.Column="1"
                    Height="30"
                    HorizontalAlignment="Stretch"
                    VerticalAlignment="Center">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto" />
                    </Grid.ColumnDefinitions>
                    <TextBlock
                        Padding="5,0,0,0"
                        HorizontalAlignment="Stretch"
                        VerticalAlignment="Center"
                        Foreground="Gray"
                        Text="Search">
                        <TextBlock.Visibility>
                            <MultiBinding Converter="{StaticResource MultiStringToVisibilityConverter}">
                                <Binding ElementName="searchBox" Path="Text.IsEmpty" />
                                <Binding ElementName="searchBox" Path="IsFocused" />
                            </MultiBinding>
                        </TextBlock.Visibility>
                    </TextBlock>

                    <TextBox
                        x:Name="searchBox"
                        Width="{Binding ActualWidth, ElementName=TextBlockGrid}"
                        Padding="5,0,0,0"
                        HorizontalAlignment="Stretch"
                        HorizontalContentAlignment="Stretch"
                        VerticalContentAlignment="Center"
                        Background="Transparent"
                        BorderBrush="Transparent"
                        BorderThickness="0"
                        Text="{Binding SearchIndicator, UpdateSourceTrigger=PropertyChanged}" />
                </Grid>
            </Grid>
        </Border>
    </Grid>

    <ItemsControl
        Grid.Row="1"
        MinWidth="150"
        HorizontalAlignment="Stretch"
        VerticalAlignment="Stretch"
        Background="White"
        BorderThickness="0"
        ItemsSource="{Binding MainValues, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">

        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Expander IsExpanded="True">
                    <Expander.Header>
                        <Grid>
                            <TextBlock x:Name="keys" Text="{Binding Key}" />
                        </Grid>
                    </Expander.Header>
                    <ListBox
                        Margin="15,0,0,0"
                        BorderThickness="0"
                        DisplayMemberPath="Name"
                        ItemsSource="{Binding Values}" />
                </Expander>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>

</Grid>

You are not binding to ItemsView at all.

Replace

ItemsSource="{Binding MainValues, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">

with

ItemsSource="{Binding ItemsView}">

Also change the ItemsView implementation to

public ICollectionView ItemsView
{            
   get { return CollectionViewSource.GetDefaultView(MainValues); }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM