简体   繁体   中英

ObservableCollection not working as expected, mvvm light

What I did is a search function to search for events. My problem here is that there's a property that fires up when i enter the page and set my ObservableCollection count to 0, but there are events details in my database. How this should have work is that when i enter on the page there is a relay command that executes and retrieve all data from database and place it into the observable collection and display the event names into a ListBox . It I only when I enter a character then after it detect the events.

Here are my codes:

xaml:

<Interactivity:Interaction.Behaviors>
    <Core:EventTriggerBehavior EventName="Loaded">
        <Core:InvokeCommandAction Command="{Binding Searchfromhomepage.EventSearch, Mode=OneWay}"/>
    </Core:EventTriggerBehavior>
</Interactivity:Interaction.Behaviors>

<Grid x:Name="LayoutRoot">

    <Grid.ChildrenTransitions>
        <TransitionCollection>
            <EntranceThemeTransition/>
        </TransitionCollection>
    </Grid.ChildrenTransitions>

    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <!--<TextBox  x:Name="txtSearch" Background="White"  Text="{Binding Path=HomePage.TxtEntered, UpdateSourceTrigger=PropertyChanged}"  FontSize="30"  Height="57" Margin="19,10,19,0" Grid.Row="1" />-->


    <TextBox x:Name="txtTest2"  Text="{Binding Searchfromhomepage.Filter, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />

    <Grid Grid.Row="1" x:Name="ContentRoot" Margin="19,9.667,19,0">
        <ListBox Background="Black"  x:Name="listBox" FontSize="26" Margin="0,10,0,0"  ItemsSource="{Binding Searchfromhomepage.FilteredNames, Mode=OneWay}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock x:Name="txtEventName" TextWrapping="Wrap" Text="{Binding EventName}" Tapped="txtEventName_Tapped" IsTapEnabled="True" Foreground="White" Width="300" Margin="10,15,0,0" Height="55"/>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>

viewmodel codes:

private static ObservableCollection<Event> _searchEventCollection = new ObservableCollection<Event>();

public static ObservableCollection<Event> SearchEventCollection
{
    get { return _searchEventCollection; }
    set { _searchEventCollection = value; }
}

//search from homepage event section
private RelayCommand _eventSearch;
/// <summary>
/// Gets the EventSearch.
/// </summary>
public RelayCommand EventSearch
{
    get
    {  return _eventSearch
            ?? (_eventSearch = new RelayCommand(
            async () =>
            {
                SearchEventCollection.Clear();
                var eventList = await App.MobileService.GetTable<Event>().ToListAsync();

                foreach (Event ename in eventList)
                {
                    SearchEventCollection.Add(new Event
                    {
                        Id = ename.Id,
                        EventName = ename.EventName,
                        Date = ename.Date,
                        Location = ename.Location,
                        Desc = ename.Desc
                    });
                }
            }));
    }
}


private string filter;
public String Filter
{
    get
    {
        return this.filter;
    }
    set
    {
        this.filter = value;
        RaisePropertyChanged("FilteredNames");
    }
}
public List<Event> FilteredNames
{
    get
    {
        if (filter == "") 
        {
            return SearchEventCollection.ToList();
        }
        else
        {
            return (from name in SearchEventCollection where name.EventName.ToUpper().StartsWith(filter.ToUpper()) select name).ToList();
        }
    }
}
public searchfromhomepageViewModel()
{ 
    filter = "";
}

在您的视图模型代码中,设置_eventSearch命令,如下所示

public RelayCommand _eventSearch = new RelayCommand(EventSearch);

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