locked
Mvvm Light binding FullName RRS feed

  • Question

  • In an Mvvm Light application in the ViewModel I write:

    public class CustomerViewModel : ViewModelBase
        {
            ...
            ...
            private string _lastname = string.Empty;
            public string Lastname
            {
                get
                {
                    return _lastname;
                }
    
                set
                {
                    Set(() => Lastname, ref _lastname, value);
                }
            }
    
            private string _firstname = string.Empty;
            public string FirstName
            {
                get
                {
                    return _firstname;
                }
    
                set
                {
                    Set(() => FirstName, ref _firstname, value);
                }
            }
    But if I need to add "FullName" as sum (as text) FirstName and Lastname, i write:
    public string FullName
            {
                get
                {
                    return string.Format("{0} {1}", Lastname, FirstName);
                }
            }

    and I add a RaisePropertyChanged (FullName) to Lastname and Name

    <TextBox Text="{Binding FullName, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"/>

    the FullName TextBox does not display anything.

    While the TextBox Lastname and FirstName are working properly.




    Tuesday, March 31, 2015 4:08 PM

Answers

  • You must raise the PropertyChanged event for the FullName property for any element bound to this property to get updated in the view.

    You could raise the PropertyChanged event for the FullName property in the setters for the other two properties:

    public class CustomerViewModel : ViewModelBase
        {
            private string _lastname = string.Empty;
            public string Lastname
            {
                get
                {
                    return _lastname;
                }
    
                set
                {
                    Set(() => Lastname, ref _lastname, value);
                    RaisePropertyChanged("FullName");
                }
            }
    
            private string _firstname = string.Empty;
            public string FirstName
            {
                get
                {
                    return _firstname;
                }
    
                set
                {
                    Set(() => FirstName, ref _firstname, value);
                    RaisePropertyChanged("FullName");
                }
            }
    
            public string FullName
            {
                get
                {
                    return string.Format("{0} {1}", Lastname, FirstName);
                }
            }
        }
    

    Then the binding should get updated as expected.

    Please upload a reproducable sample of your issue to OneDrive and post the link to it here if you need any further help as we cannot guess what is going on or what you are doing wrong in your application without having seen all of your code.

    Please remember to mark helpful posts as answer to close your threads and then start a new thread if you have a new question. Please don't ask several questions in the same thread.

    • Marked as answer by Barry Wang Wednesday, April 1, 2015 9:29 AM
    Tuesday, March 31, 2015 4:37 PM
  • Thank you, as always, for your quick reply. In fact I understood my mistake. Let me explain.

    I had previously already written what you suggested, but in the wrong way:

    RaisePropertyChanged(FullName);
    

    Instead of:

    RaisePropertyChanged("FullName");
    
    

    Better yet:

    RaisePropertyChanged(() => FullName);

    Ok. Thank you



    • Edited by piedatt80 Tuesday, March 31, 2015 4:55 PM error
    • Marked as answer by Barry Wang Wednesday, April 1, 2015 9:29 AM
    Tuesday, March 31, 2015 4:53 PM

All replies

  • You must raise the PropertyChanged event for the FullName property for any element bound to this property to get updated in the view.

    You could raise the PropertyChanged event for the FullName property in the setters for the other two properties:

    public class CustomerViewModel : ViewModelBase
        {
            private string _lastname = string.Empty;
            public string Lastname
            {
                get
                {
                    return _lastname;
                }
    
                set
                {
                    Set(() => Lastname, ref _lastname, value);
                    RaisePropertyChanged("FullName");
                }
            }
    
            private string _firstname = string.Empty;
            public string FirstName
            {
                get
                {
                    return _firstname;
                }
    
                set
                {
                    Set(() => FirstName, ref _firstname, value);
                    RaisePropertyChanged("FullName");
                }
            }
    
            public string FullName
            {
                get
                {
                    return string.Format("{0} {1}", Lastname, FirstName);
                }
            }
        }
    

    Then the binding should get updated as expected.

    Please upload a reproducable sample of your issue to OneDrive and post the link to it here if you need any further help as we cannot guess what is going on or what you are doing wrong in your application without having seen all of your code.

    Please remember to mark helpful posts as answer to close your threads and then start a new thread if you have a new question. Please don't ask several questions in the same thread.

    • Marked as answer by Barry Wang Wednesday, April 1, 2015 9:29 AM
    Tuesday, March 31, 2015 4:37 PM
  • Thank you, as always, for your quick reply. In fact I understood my mistake. Let me explain.

    I had previously already written what you suggested, but in the wrong way:

    RaisePropertyChanged(FullName);
    

    Instead of:

    RaisePropertyChanged("FullName");
    
    

    Better yet:

    RaisePropertyChanged(() => FullName);

    Ok. Thank you



    • Edited by piedatt80 Tuesday, March 31, 2015 4:55 PM error
    • Marked as answer by Barry Wang Wednesday, April 1, 2015 9:29 AM
    Tuesday, March 31, 2015 4:53 PM