简体   繁体   English

在用户控件XAML内绑定

[英]Binding inside a Usercontrol XAML

I created a Usercontrol, with a property of type "User" which contains properties that I would like to bind to controls Inside the Usercontrol. 我创建了一个Usercontrol,其属性类型为“ User”,其中包含我想绑定到Usercontrol内部控件的属性。

Following a blog post ( http://blog.jerrynixon.com/2013/07/solved-two-way-binding-inside-user.html ), I managed to do my bindings, but when I update the property "User" Inside the view model of the page including my Usercontrol, the fields aren't updated. 在发布博客文章( http://blog.jerrynixon.com/2013/07/solved-two-way-binding-inside-user.html )之后,我设法进行绑定,但是当我更新属性“ User”时在包含我的Usercontrol的页面视图模型中,这些字段未更新。

Here is my code : The Usercontrol.xaml.cs 这是我的代码:Usercontrol.xaml.cs

public sealed partial class WebTeamUserControl : UserControl
{


    public WebTeamUserControl()
    {
        this.InitializeComponent();
        (this.Content as FrameworkElement).DataContext = this;
    }

    public static readonly DependencyProperty UserProperty =
        DependencyProperty.Register("User", typeof(User), typeof(WebTeamUserControl), new PropertyMetadata(new User()));

    public User User
    {
        get { return (User)GetValue(UserProperty); }
        set { SetValueDp(UserProperty, value); }
    }


    public string Username { get { return User.prenom + " " + User.nom; } }

    public string Nickname { get { return User.pseudo; } }

    // Some other fields

    public event PropertyChangedEventHandler PropertyChanged;
    void SetValueDp(DependencyProperty property, object value, [System.Runtime.CompilerServices.CallerMemberName] String p = null)
    {
        SetValue(property, value);
        if(PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(p));
        }
    }

}

The binding is the XAML file is done with {Binding NameOfTheProperty} 绑定是XAML文件是通过{Binding NameOfTheProperty}

In the page where I am using this control, I bind with <controls:WebTeamUserControl User="{Binding User, Mode=TwoWay}" /> , and here is the page's datacontrol: 在使用此控件的页面中,我绑定了<controls:WebTeamUserControl User="{Binding User, Mode=TwoWay}" /> ,这是页面的datacontrol:

class ProfileViewModel : ViewModelBase
{
    public User User { get; set; }

    public ProfileViewModel()
    {
        if(_isInDesignMode)
        {
            User = new User();
        }
        GetAppUser();
    }

    /// <summary>
    /// Chargement de l'utilisateur
    /// </summary>
    public async void GetAppUser()
    {
        // Creating dummy object for design mode
        if (_isInDesignMode)
        {
            User = new User();
        }
        else
        {
            //On charge depuis la mémoire locale l'utilisateur
            try
            {
                var roamingSettings = Windows.Storage.ApplicationData.Current.RoamingSettings;
                string userNickname = (string)roamingSettings.Values["user_nickname"];

                User = await User.LoadUserFromTemporaryStorage(userNickname);
                RaisePropertyChanged("User");
            }
            catch
            {
                //L'utilisateur n'existe pas dans la mémoire locale, on se déconnecte
                Resources.APIWebTeam.Connection.Disconnect();
            }
        }

        RaisePropertyChanged("Username");
        RaisePropertyChanged("Nickname");
        RaisePropertyChanged("Promo");
        RaisePropertyChanged("Groupe");
        RaisePropertyChanged("DateDeNaissance");
    }
}

Any idea why it doens't work? 知道为什么它不起作用吗?

The fields Username and Fieldname are no dependency properties. 字段用户名和字段名不是依赖项属性。 If you simply bind to the properties on the User object, everything should work (I've just double checked local). 如果仅绑定到User对象上的属性,则所有内容均应正常工作(我已经仔细检查了local)。

<TextBlock Text="{Binding User.Name}" />
<TextBlock Text="{Binding User.LastName}" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM