Я не могу заставить свой xaml связываться с моей моделью представления.В моем viewModel есть ObservableCollection класса INotifyPropertyChanged, который содержит данные о получателе.Вот мой класс Recepie:
namespace WP7SQLiteClient.Model
{
public class MainViewModelItem : INotifyPropertyChanged
{
string _title, _subTitle, _imageUriPath;
string title
{
get
{
return _title;
}
set
{
_title = value;
NotifyPropertyChanged("title");
}
}
string subTitle
{
get
{
return _subTitle;
}
set
{
_subTitle = value;
NotifyPropertyChanged("subTitle");
}
}
string imageUriPath
{
get
{
return _imageUriPath;
}
set
{
_imageUriPath = value;
NotifyPropertyChanged("imageUriPath");
}
}
public MainViewModelItem(string title, string subtitle, string imageuripath)
{
this.title = title;
this.subTitle = subtitle;
this.imageUriPath = imageuripath;
}
public event PropertyChangedEventHandler PropertyChanged;
protected void NotifyPropertyChanged(String info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
}
}
И мой ViewModel, который содержит список получателей:
namespace WP7SQLiteClient.ViewModel
{
public class PanoramaViewModel : INotifyPropertyChanged
{
public ObservableCollection<MainViewModelItem> _recepiesList;
public ObservableCollection<MainViewModelItem> recepiesList
{
get
{
return _recepiesList;
}
set
{
_recepiesList = value;
NotifyPropertyChanged("recepiesList");
}
}
public PanoramaViewModel()
{
this.recepiesList = new ObservableCollection<MainViewModelItem>();
}
public bool IsDataLoaded
{
get;
private set;
}
public void LoadData()
{
this.recepiesList.Add(new MainViewModelItem("Classics", "", ""));
this.recepiesList.Add(new MainViewModelItem("Perfect Pasta", "", ""));
this.recepiesList.Add(new MainViewModelItem("Favorites", "", ""));
this.recepiesList.Add(new MainViewModelItem("Snacks & Antipasti", "", ""));
this.recepiesList.Add(new MainViewModelItem("Desserts", "", ""));
this.recepiesList.Add(new MainViewModelItem("3 minutes recipes", "", ""));
this.IsDataLoaded = true;
}
private string _sampleProperty = "Sample Runtime Property Value";
/// <summary>
/// Sample ViewModel property; this property is used in the view to display its value using a Binding
/// </summary>
/// <returns></returns>
public string SampleProperty
{
get
{
return _sampleProperty;
}
set
{
if (value != _sampleProperty)
{
_sampleProperty = value;
NotifyPropertyChanged("SampleProperty");
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected void NotifyPropertyChanged(String info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
}
}
В моем MainPage.xaml (он находится в корне проекта, viewModelнаходится в папке ViewModel, а модель находится в папке Model) мой список объявлен следующим образом:
<ListBox x:Name="recepiesList" ItemTemplate="{StaticResource ListViewModelTemplate}" >
</ListBox>
Шаблон расположен в App.xaml и является правильным точно.Он использует такие вещи, как {Binding title}
В MainPage.cs я пытаюсь связать модель представления со страницей, используя:
public static PanoramaViewModel viewModel = null;
public static PanoramaViewModel ViewModel
{
get
{
// Delay creation of the view model until necessary
if (viewModel == null)
viewModel = new PanoramaViewModel();
return viewModel;
}
}
public MainPage()
{
InitializeComponent();
ViewModel.LoadData();
DataContext = ViewModel;
}
Но это не работает, и отладчик не вызываетошибка.Как я могу правильно связать viewmodel с xaml?
ОБНОВЛЕНИЕ мой шаблон выглядит следующим образом:
<DataTemplate x:Key="ListViewModelTemplate"> <!-- for recent recepies-->
<Grid Width="400" Height="80" VerticalAlignment="Center">
<StackPanel Orientation="Vertical">
<Border CornerRadius="0" x:Name="brdTesat" BorderBrush="Black" BorderThickness="1" Width="80" Height="80">
<Border.Background>
<ImageBrush x:Name="backgroundImaageBrush" Stretch="Fill">
<ImageBrush.ImageSource>
<BitmapImage x:Name="bmapBackground" UriSource="{Binding imageUriPath}" >
</BitmapImage>
</ImageBrush.ImageSource>
</ImageBrush>
</Border.Background>
</Border>
<StackPanel>
<TextBlock TextAlignment="Left" Margin="7,4,4,4" Text="{Binding title}" TextWrapping="Wrap"></TextBlock>
<TextBlock TextAlignment="Left" Margin="7,4,4,4" Text="DA" TextWrapping="Wrap"></TextBlock>
</StackPanel>
</StackPanel>
</Grid>
</DataTemplate>
ОБНОВЛЕНИЕ 2
Я изменил код списка в::
<ListBox x:Name="recepiesList" ItemsSource="{Binding recepiesList}" > </ListBox>
Без шаблона я получаю список [project_name].Model.MainViewModelItem
, поэтому я думаю, что есть проблема с шаблоном .. Что я делаю не так?