我正在尝试在windows10 C#应用程序中实现新的编译绑定,我关注了MSVA视频和绑定工作,但当我做了一些更改时,用户界面不会更新。例如,当我在profilesView中添加新的配置文件时,或者当我在配置文件视图中更新名称时。下面是我的代码:
profile.cs
public string Name { get; set; }
public string Surname { get; set; }
public string FullName { get; set; }profileviewmodel.cs
public class ProfileViewModel : BindableBase
{
private Profile defaultProfile = new Profile { };
public Profile DefaultProfile {
get { return this.defaultProfile; }
set { this.SetProperty(ref this.defaultProfile, value); }
}profilesviewmodel.cs
public class ProfilesViewModel : BindableBase
{
private ObservableCollection<Profile> profiles = new ObservableCollection<Profile> { };
public ObservableCollection<Profile> Profiles {
get { return this.profiles; }
set { this.SetProperty(ref this.profiles, value); }
}profilespage.xaml
xmlns:vm="using:vCard.ViewModels">
<Page.DataContext>
<vm:ProfilesViewModel/>
</Page.DataContext>
<ListView ItemsSource="{x:Bind ViewModel.Profiles}" xmlns:model="using:vCard.Models">
<ListView.ItemTemplate>
<DataTemplate x:DataType="model:Profile">
<Grid>
<TextBlock Text="{x:Bind Name, Mode=TwoWay}"/>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>profilespage.xaml.cs
public ProfilesViewModel ViewModel { get; set; }
public ProfilesPage2()
{
this.ViewModel = new ProfilesViewModel();
this.InitializeComponent();
this.DataContextChanged += (s,e) => { ViewModel = DataContext as ProfilesViewModel; };
ViewModel.Profiles.Add(new Profile { Name = "aaa" });
}profilepage.xaml
xmlns:vm="using:vCard.ViewModels">
<Page.DataContext>
<vm:ProfileViewModel/>
</Page.DataContext>
<StackPanel>
<TextBlock Text="Profile" Margin="10"/>
<TextBox Text="{Binding DefaultProfile.Name, Mode=TwoWay}" TextChanged="Name_TextChanged"/>
<TextBox Text="{Binding DefaultProfile.Surname, Mode=TwoWay}" TextChanged="Name_TextChanged"/>
<TextBox Text="{Binding DefaultProfile.FullName, Mode=TwoWay}"/>
</StackPanel>profilepage.xaml.cs
public ProfileViewModel ViewModel { get; set; }
public ProfilePage2()
{
this.ViewModel = new ProfileViewModel();
this.InitializeComponent();
// Setup the DataContext
this.DataContextChanged += (s, e) => { ViewModel = DataContext as ProfileViewModel; };
ViewModel.DefaultProfile.Name = "aaa";
}谢谢
发布于 2015-10-03 04:54:10
下面是我如何根据你的场景来做这件事。我有点不同的方法,但足够清楚,我希望:
仅当要绑定UI控件上的属性时,才使用implement profile.cs INotifyPropertyChanged
public class Profile : INotifyPropertyChanged
{
public string name;
public string surname;
public string fullname;
public string Name { get { return name; } set { name = value; OnPropertyChanged("Name"); } }
public string Surname { get { return surname; } set { surname = value; OnPropertyChanged("Surname"); } }
public string FullName { get { return fullname; } set { fullname = value; OnPropertyChanged("Fullname"); } }
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}profileviewmodel.cs **已更新
public ObservableCollection<Profile> Profiles { get; set;}
public ProfilesViewModel()
{
Profiles = new ObservableCollection<Profile>();
}profilepage.xaml
<ListView ItemsSource="{Binding Profiles}" >
<ListView.ItemTemplate>
<DataTemplate>
<Grid>
<!-- The TwoWay binding is for updating the object through UI -->
<!-- If you need TwoWay you must use TextBox not TextBlock -->
<TextBlock Text="{Binding Name, Mode=OneWay}"/>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>profilepage.xaml.cs
public ProfilesViewModel ViewModel = new ProfilesViewModel();
public ProfilePage()
{
this.InitializeComponent();
this.DataContext = ViewModel;
ViewModel.Profiles.Add(new Profile { Name = "aaa" });
ViewModel.Profiles.Add(new Profile { Name = "bbb" });
}发布于 2015-09-07 21:19:04
尝试在您的代码中实现INotifyPropertyChanged
profile.cs
public class Profile : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this,
new PropertyChangedEventArgs(propertyName));
}
}
private string name = string.Empty;
public string Name
{
get { return name; }
set { name = value; NotifyPropertyChanged("Name"); }
}
private string surname = string.Empty;
public string Surname
{
get { return surname; }
set { surname = value; NotifyPropertyChanged("Surname"); }
}
private string fullName = string.Empty;
public string FullName
{
get { return fullName; }
set { fullName = value; NotifyPropertyChanged("FullName"); }
}
}https://stackoverflow.com/questions/32415874
复制相似问题