问题描述
我无法将我的查看模型
绑定到我的视图
。我是一个具有 MVVM
的初学者,但我相信我正在实现我的系统(几乎)。我有一个模型
包含数据,我在查看模型
中获取,然后当我的页面被导航到,我试图抓住查看模型
数据并将其绑定到查看
。
I am having trouble binding my View Model
to my View
. I am a beginner with MVVM
, but I believe I am implementing my system (almost) correctly. I have a Model
that contains data, which I am getting in my View Model
, and then when my page is navigated to, I am attempting to grab that View Model
data and binding it to the View
.
我的问题是我的查看$ c $中有一个
ListBox
c>每个项目有3个对象,我似乎无法正确绑定到我的列表中的每个项目。
My issue is that I have a ListBox
in my View
with 3 objects per item, and I cannot seem to bind to it correctly for each item in my list.
MainPage.xaml / p>
MainPage.xaml
<ListBox x:Name="FavoritesListBox" ItemsSource="{Binding FavoriteItems}"
SelectionChanged="FavoritesListBox_SelectionChanged">
<StackPanel Orientation="Horizontal" Margin="12,0,12,0">
<Image x:Name="favicon" Source="{Binding Favicon}"
Width="50" Height="50"/>
<StackPanel>
<TextBlock x:Name="favoritesName" Text="{Binding Name}"
FontSize="{StaticResource PhoneFontSizeExtraLarge}"/>
<TextBlock x:Name="favoritesAddress"
Text="{Binding Address}" Margin="12,0,0,0"/>
</StackPanel>
</StackPanel>
</ListBox>
MainPage.xaml.cs
public FavoritesPage()
{
InitializeComponent();
// Set the data context of the listbox control to the sample data
FavoritesListBox.DataContext = App.ViewModel;
}
App.xaml.cs
private static MainViewModel viewModel = null;
public static MainViewModel ViewModel
{
get
{
// Delay creation of the view model until necessary
if (viewModel == null)
viewModel = new MainViewModel();
return viewModel;
}
}
MainViewModel.cs / p>
MainViewModel.cs
public ObservableCollection<ItemViewModel> FavoriteItems { get; private set; }
public MainViewModel()
{
//FavoriteItems = new ObservableCollection<ItemViewModel>();
FavoriteItems = Settings.FavoritesList.Value;
}
Settings.cs(The Model) p>
Settings.cs (The Model)
public static Setting<ObservableCollection<ItemViewModel>> FavoritesList =
new Setting<ObservableCollection<ItemViewModel>>(
"Favorites",
new ObservableCollection<ItemViewModel>());
ItemViewModel.cs
private string _favicon;
public string Favicon
{
get
{
return _favicon;
}
set
{
if (value != _favicon)
{
_favicon = value;
NotifyPropertyChanged("Favicon");
}
}
}
private string _name;
public string Name
{
get
{
return _name;
}
set
{
if (value != _name)
{
_name = value;
NotifyPropertyChanged("Name");
}
}
}
private string _address;
public string Address
{
get
{
return _address;
}
set
{
if (value != _address)
{
_address = value;
NotifyPropertyChanged("Address");
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (null != handler)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
..这是我在哪里和如何保存每个项目(应该在 ItemViewModel
中列出三个属性
..and this is where and how I am saving each item (which should have three properties listed in the ItemViewModel
void addToFavorites_Click(object sender, EventArgs e)
{
var favoriteItem =
new ItemViewModel{
Favicon = "",
Name = "",
Address = TheBrowser.currentUrl() };
Settings.FavoritesList.Value.Add(favoriteItem);
}
其中 FavoritesList
使用包含3个对象的 ItemViewModel
填充,列表正在正确填充因为在调试期间,我可以看到 FavoritesList
中的实体,但是我在视图模型中调用这些实体的问题
在视图中的
ListBox
中显示
Where FavoritesList
is populated using an ItemViewModel
containing 3 objects. The list is being populated correctly because during debugging I can see the entities in FavoritesList
, but I am having an issue calling these entities in the view model
to show up in my ListBox
in the view
?
我相信我是不正确的,但我不是sur
I believe I am binding incorrectly but I'm not sure how to fix this?
推荐答案
在您的XAML中,绑定到路径名称
和地址
您是否在 ItemViewModel
中定义了这2个属性?
In your XAML you bind to paths Name
and Address
do you have these 2 properties defined in your ItemViewModel
?
正确阅读代码后更新:
您没有更新列表框项目的数据表。这是你需要做的:
You are not updating the datatemplate of the Items of the Listbox. This is what you need to do:
<ListBox x:Name="FavoritesListBox" ItemsSource="{Binding FavoriteItems}" SelectionChanged="FavoritesListBox_SelectionChanged">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Margin="12,0,12,0">
<Image x:Name="favicon" Source="{Binding Favicon}" Width="50" Height="50"/>
<StackPanel>
<TextBlock x:Name="favoritesName" Text="{Binding Name}" FontSize="{StaticResource PhoneFontSizeExtraLarge}"/>
<TextBlock x:Name="favoritesAddress" Text="{Binding Address}" Margin="12,0,0,0"/>
</StackPanel>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
这篇关于如何将视图模型绑定到视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!