【问题标题】:ObservableCollection not working as expected, mvvm lightObservableCollection 未按预期工作,mvvm light
【发布时间】:2015-07-26 18:43:24
【问题描述】:

我所做的是一个搜索功能来搜索事件。 我的问题是,当我进入页面并将我的ObservableCollection 计数设置为 0 时,会触发一个属性,但我的数据库中有事件详细信息。 这应该如何工作是,当我在页面上输入时,有一个中继命令执行并从数据库中检索所有数据并将其放入可观察的集合中并将事件名称显示到ListBox 中。只有当我输入一个字符然后它检测到事件之后。

这是我的代码:

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>

视图模型代码:

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 = "";
}

【问题讨论】:

  • 页面的DataContext是什么?你在什么时候设置它?加载页面时是否触发了 EventSearch 命令?输出窗口中是否有任何绑定错误?
  • 页面加载时 EventSearch 会启动。我不知道绑定错误,但在我第一次进入页面时没有显示任何内容,但数据库中仍有数据
  • 表示绑定到EventSearch命令是OK的。能否在“foreach(事件列表中的事件名称)”后调试并下断点,看看 SearchEventCollection 是否包含所有项?
  • 是的 SearchEventCollection 包含所有项目但不显示任何信息。
  • 为什么ListBox的ItemsSource没有绑定SearchEventCollection?

标签: c# windows-phone-8.1 mvvm-light observablecollection


【解决方案1】:

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

public RelayCommand _eventSearch = new RelayCommand(EventSearch);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-04
    • 2022-01-24
    • 2015-05-11
    • 2020-05-15
    相关资源
    最近更新 更多