C# 将viewmodel链接到视图
我无法使我的xaml与我的viewmodel绑定。我的viewModel中有一个INotifyPropertyChanged类的ObservableCollection,该类保存有关recepie的数据。这是我的课程:C# 将viewmodel链接到视图,c#,silverlight,windows-phone-7,C#,Silverlight,Windows Phone 7,我无法使我的xaml与我的viewmodel绑定。我的viewModel中有一个INotifyPropertyChanged类的ObservableCollection,该类保存有关recepie的数据。这是我的课程: namespace WP7SQLiteClient.Model { public class MainViewModelItem : INotifyPropertyChanged { string _title, _subTitle, _image
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));
}
}
}
}
但它不起作用,调试器也不会引发错误。如何将viewmodel与xaml正确链接
更新我的模板如下所示:
<ListBox x:Name="recepiesList" ItemTemplate="{StaticResource ListViewModelTemplate}" >
</ListBox>
<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\u name].Model.MainViewModelItem
的列表,因此我认为模板有问题。。我做错了什么 我们在一个项目中使用MEF,并在view.xaml.cs中将视图模型与以下代码链接起来:
[Import]
public ConnectionStringSetupViewModel ViewModel
{
get { return DataContext as ConnectionStringSetupViewModel; }
set { DataContext = value; }
}
这允许在创建目录时满足导入要求。如果您不使用MEF,您可以使用上面相同的代码而无需导入,但在创建视图时,您必须为其分配viewmodel类的新实例。您需要将ListBox绑定到数据。所以,这应该对你有用
<ListBox x:Name="recepiesList" ItemsSource="{Binding recepiesList}" ItemTemplate="{StaticResource ListViewModelTemplate}" />
如何分配viewmodel类的新实例?我不知道你是什么意思PanaromaView p=newpanaromaview();p、 ViewModel=新的ViewModel();我添加了一个更新2,请看,这正是您在没有指定模板的情况下将得到的。我认为,一旦你将ItemsSource和ItemsTemplate结合起来,你就会达到你需要的位置。这不起作用。。我得到:在mscorlib.dll System.Windows中发生了“System.IO.FileNotFoundException”类型的第一次意外异常。数据错误:BindingExpression路径错误:“在”WP7SQLiteClient.Model.MainViewModelItem“WP7SQLiteClient.Model.MainViewModelItem”上未找到“title”属性(HashCode=65782377i.e.字幕字符串没有出现,因为我没有绑定它,我给它一个静态字符串,因为你的viewmodel的属性是不公开的!公开它们并高兴:)啊,关于公开,你是对的,但我仍然没有任何显示没有错误,但没有显示列表框中的项目..输出中没有错误
<ListBox x:Name="recepiesList" ItemsSource="{Binding recepiesList}" ItemTemplate="{StaticResource ListViewModelTemplate}" />