【发布时间】:2014-02-14 23:56:54
【问题描述】:
我正在尝试将应该是一个非常基本的 MVVM 示例放在一起,但我无法让它工作。基本上,我想将ObservableCollection 绑定到ListBox,并为用户提供搜索选项以搜索其他项目。搜索时,ListBox 应该被刷新,因为集合会改变。这是我的代码:
型号:
public class Item
public string Name { get; set; }
}
视图模型:
public class ViewModel : INotifyPropertyChanged
{
private ObservableCollection<Item> _items { get; set; }
public ObservableCollection<Item> Items
{
get { return _items; }
set
{
_items = value;
RaisePropertyChanged("Items");
}
}
public void GetDefaultItems()
{
ObservableCollection<Item> temp = new ObservableCollection<Item>();
temp.Add(new Item() { Name = "abc" + " 1" });
temp.Add(new Item() { Name = "def" + " 2" });
temp.Add(new Item() { Name = "ghi" + " 3" });
Items = temp;
}
public void Search(string query)
{
ObservableCollection<Item> temp = new ObservableCollection<Item>();
temp.Add(new Item() { Name = query + " 1" });
temp.Add(new Item() { Name = query + " 2" });
temp.Add(new Item() { Name = query + " 3" });
Items = temp;
}
public event PropertyChangedEventHandler PropertyChanged;
private void RaisePropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
查看:
<Grid x:Name="LayoutRoot">
<ListBox ItemsSource="{Binding}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock x:Name="Name" Text="{Binding Name}" Grid.Column="0" HorizontalAlignment="Left" VerticalAlignment="Center" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
MainPage.xaml:
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<StackPanel>
<TextBox x:Name="txtSearch"/>
<TextBlock Text="Items:" />
<views:ItemView x:Name="ItemsOnPage" />
</StackPanel>
</Grid>
最后是 MainPage.xaml.cs:
public partial class MainPage : PhoneApplicationPage
{
private ViewModel vm;
// Constructor
public MainPage()
{
InitializeComponent();
txtSearch.KeyUp += txtSearch_KeyUp;
vm = new ViewModel();
}
void txtSearch_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
vm.Search(txtSearch.Text);
}
}
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
base.OnNavigatedTo(e);
vm.GetDefaultItems();
ItemsOnPage.DataContext = vm.Items;
}
}
所以发生的情况是,我可以看到第一次加载默认项目,但是当我搜索时,列表没有刷新。它现在都是硬编码的,所以如果搜索确实有效我应该看到 3 个项目,无论他们搜索什么。
我注意到的是,如果我在 ViewModel 中的 RaisePropertyChanged 处设置断点,this.PropertyChanged总是为空,因此它永远不会在 if 语句中出现.我已经看到在模型上使用INotifyPropertyChanged 的示例,但在这种情况下,由于我需要在集合更改时收到通知,因此在视图模型上使用似乎是正确的。这可能是错误的,但我不确定如何设置它。
谁能看出我做错了什么?
【问题讨论】:
-
尝试将
ItemsOnPage.DataContext = vm.Items;放入 MainPage 构造函数中。 -
所以只是将它从
OnNavigatedTo移到构造函数中?我刚才试过了,没什么区别。 -
也许你需要为
windows-phone-8举办一些其他活动 -
我实现 Xamarin 表单视图模型的好例子
标签: c# mvvm windows-phone-8 observablecollection