在Usercontrol XAML中绑定

时间:2015-10-14 15:07:44

标签: c# wpf xaml binding user-controls

我创建了一个Usercontrol,其属性类型为“User”,其中包含我要绑定到Usercontrol内部控件的属性。

在博客文章(http://blog.jerrynixon.com/2013/07/solved-two-way-binding-inside-user.html)之后,我设法进行了绑定,但是当我更新属性“User”在包含我的Usercontrol的页面的视图模型中时,字段不会更新。

这是我的代码: 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));
        }
    }

}

绑定是XAML文件使用{Binding NameOfTheProperty}

完成的

在我使用此控件的页面中,我与<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");
    }
}

知道它为什么不起作用?

1 个答案:

答案 0 :(得分:2)

字段Username和Fieldname不是依赖项属性。如果你只是绑定到User对象的属性,一切都应该工作(我只是双重检查本地)。

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