我希望這裏的任何人都可以幫助我動態綁定數據。當我在一個按鈕上添加一個列表項時,它不會在我的頁面中更新,這也適用於向int添加一個值。然而,當我輸入一個文本框時int會改變......所以我不明白髮生了什麼問題。我也不知道這是否正確。windows store動態數據綁定列表和其他類型
所以我qeustions的快速概覽:
- 爲什麼這個不行?
- 這是正確的方法嗎?
- 有人可以告訴我如何使它工作?
假定的錯誤位置:MainViewModel類中的NotifyPropertyChanged方法。 這是我項目中的所有代碼,只是說它在另一個地方出錯了,而不是我想象的那樣。
我的XAML頁面:
<Page
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:databinding_unittest0"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:ViewModels="using:databinding_unittest0.ViewModels"
x:Class="databinding_unittest0.MainPage"
mc:Ignorable="d">
<Page.DataContext>
<ViewModels:MainViewModel/>
</Page.DataContext>
<Grid>
<TextBox Text="{Binding Testvar, Mode=TwoWay}" Height="100" Margin="110,10,1023,658"/>
<TextBlock Text="{Binding Testvar}" Height="100" Margin="110,115,1023,553"/>
<ListBox ItemsSource="{Binding Persons}" Height="500" VerticalAlignment="Bottom">
<ListBox.ItemTemplate>
<DataTemplate>
<ListBoxItem Content="{Binding Fullname}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<Button Content="Add 1" HorizontalAlignment="Left" Margin="441,46,0,0" VerticalAlignment="Top" Click="Add1_Click"/>
<Button Content="Add list item" HorizontalAlignment="Left" Margin="609,46,0,0" VerticalAlignment="Top" Click="AddListItem_Click"/>
</Grid>
</Page>
C#中的頁面:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using databinding_unittest0.ViewModels;
using databinding_unittest0.Models;
// The Blank Page item template is documented at
// http://go.microsoft.com/fwlink/?LinkId=234238
namespace databinding_unittest0
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainPage : Page
{
public MainViewModel mvm { get; set; }
public MainPage()
{
this.InitializeComponent();
mvm = new MainViewModel();
}
/// <summary>
/// Invoked when this page is about to be displayed in a Frame.
/// </summary>
/// <param name="e">Event data that describes how this page was reached. The Parameter
/// property is typically used to configure the page.</param>
protected override void OnNavigatedTo(NavigationEventArgs e)
{
}
private void Add1_Click(object sender, RoutedEventArgs e)
{
mvm.Testvar = mvm.Testvar + 1;
}
private void AddListItem_Click(object sender, RoutedEventArgs e)
{
mvm.Persons.Add(new Person() { Firstname="Kurt", Lastname="Cobain"});
}
}
}
MainViewModel C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using Windows.ApplicationModel;
using databinding_unittest0.Models;
namespace databinding_unittest0.ViewModels
{
public class MainViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged = delegate { };
private List<Person> persons;
public List<Person> Persons
{
get { return persons; }
set
{
persons = value;
NotifyPropertyChanged();
}
}
private int testvar;
public int Testvar
{
get { return testvar; }
set
{
testvar = value;
NotifyPropertyChanged();
}
}
public MainViewModel()
{
Persons = new List<Person>();
Persons.Add(new Person() { Firstname = "Henk", Lastname = "Jansen" });
}
// This method is called by the Set accessor of each property.
// The CallerMemberName attribute that is applied to the optional propertyName
// parameter causes the property name of the caller to be substituted as an argument.
private void NotifyPropertyChanged([CallerMemberName] string propertyName = null)
{
if (propertyName != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
}
人物C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace databinding_unittest0.Models
{
public class Person
{
public string Firstname { get; set; }
public string Lastname { get; set; }
public string Fullname
{
get
{
return Firstname + " " + Lastname;
}
}
}
}
嘗試使用ObservableCollection <>代替List <>。 此外 - 單擊按鈕事件不能在視圖的代碼隱藏。嘗試綁定事件。 (這是MVVM) – Niewidzialny
我將如何去綁定,雖然?也感謝Allot –
看看這裏:) https://channel9.msdn.com/Series/Windows-Phone-8-1-Development-for-Absolute-Beginners/Part-18-Understanding-MVVM-ObservableCollection-T -and-INotifyPropertyChanged – Niewidzialny