我正在尝试在Windows 10 C#应用程序中实现新的编译绑定,我遵循了MSVA视频和绑定工作,但是当我进行一些更改时,UI不会更新。 例如,当我在profilesView中添加新配置文件时,或者当我在profileview中更新名称时。 这是我的代码:
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";
}
感谢
答案 0 :(得分:1)
以下是根据您的方案进行操作的方法。我有点不同的方法但很清楚,我希望理解:
profile.cs ***仅当您将绑定UI控件上的属性时才使用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" });
}
答案 1 :(得分:0)
在代码中尝试实现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"); }
}
}