将视图模型链接到视图
我无法让我的 xaml 与我的视图模型绑定。我的 viewModel 中有一个 INotifyPropertyChanged 类的 ObservableCollection,它保存有关接收器的数据。这是我的 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 位于 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
,所以我认为模板有问题。我做错了什么?
I can't get my xaml to bind with my viewmodel. I have in my viewModel a ObservableCollection of a INotifyPropertyChanged class that holds data about a recepie. Here is my Recepie class :
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));
}
}
}
}
And my ViewModel which holds the list of recepies :
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));
}
}
}
}
In my MainPage.xaml (it's in the root of the project, the viewModel is in the ViewModel folder and the Model is in the Model folder) i have my listbox declared like this :
<ListBox x:Name="recepiesList" ItemTemplate="{StaticResource ListViewModelTemplate}" >
</ListBox>
The template is located in the App.xaml and is correct for sure. It uses things like {Binding title}
In the MainPage.cs i try to link the view model with the page using :
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;
}
But it doesn't work, nor the debugger raises an error. How can i correctly link the viewmodel with the xaml ?
UPDATE my template looks like this :
<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>
UPDATE 2
i've changed my listbox code to :
<ListBox x:Name="recepiesList" ItemsSource="{Binding recepiesList}" > </ListBox>
With no template, i get a list of [project_name].Model.MainViewModelItem
, so i think there is a problem with the template.. What am i doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要将 ListBox 绑定到数据。所以,这应该对你有用。
You need to bind you ListBox to the data. So, this should work for you.
我们在项目中使用 MEF,并使用 View.xaml.cs 中的以下代码链接视图模型:
这允许在创建目录时满足导入。如果您不使用 MEF,则可以使用上面相同的代码而不进行导入,但是在创建视图时,您必须为其分配视图模型类的新实例。
We're using MEF in a project and we link the view models up with the following code in the View.xaml.cs:
This allows the import to be satisfied when the catalogs are created. If you aren't using MEF, you can use the same code above without the import, but when your view is created you'll have to assign a new instance of your viewmodel class to it.