首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >windows 10编译的绑定最佳实践

windows 10编译的绑定最佳实践
EN

Stack Overflow用户
提问于 2015-09-06 01:30:21
回答 2查看 440关注 0票数 0

我正在尝试在windows10 C#应用程序中实现新的编译绑定,我关注了MSVA视频和绑定工作,但当我做了一些更改时,用户界面不会更新。例如,当我在profilesView中添加新的配置文件时,或者当我在配置文件视图中更新名称时。下面是我的代码:

profile.cs

代码语言:javascript
复制
public string Name { get; set; }
public string Surname { get; set; }
public string FullName { get; set; }

profileviewmodel.cs

代码语言:javascript
复制
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

代码语言:javascript
复制
     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

代码语言:javascript
复制
    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

代码语言:javascript
复制
  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

代码语言:javascript
复制
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

代码语言:javascript
复制
    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";

    }

谢谢

EN

回答 2

Stack Overflow用户

发布于 2015-10-03 04:54:10

下面是我如何根据你的场景来做这件事。我有点不同的方法,但足够清楚,我希望:

仅当要绑定UI控件上的属性时,才使用implement profile.cs INotifyPropertyChanged

代码语言:javascript
复制
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 **已更新

代码语言:javascript
复制
public ObservableCollection<Profile> Profiles { get; set;}

public ProfilesViewModel()
{
    Profiles = new ObservableCollection<Profile>();
}

profilepage.xaml

代码语言:javascript
复制
<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

代码语言:javascript
复制
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
EN

Stack Overflow用户

发布于 2015-09-07 21:19:04

尝试在您的代码中实现INotifyPropertyChanged

profile.cs

代码语言:javascript
复制
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"); }
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32415874

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档