1
votes

I created a Usercontrol, with a property of type "User" which contains properties that I would like to bind to controls Inside the 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.

Here is my code : The 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}

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:

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?

1

1 Answers

1
votes

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).

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