Skip to content

[MVVM] How to correctly do TwoWay bind of a TextBox #8116

Answered by timunie
ckorn asked this question in Q&A
Discussion options

You must be logged in to vote

Hi @ckorn

I see a couple of things here:

    public class MainWindowViewModel : ViewModelBase
    {
        public string Log 
        {  
             get => log; 
             set => this.RaiseAndSetIfChanged(ref _searchTerm, value);  // this will update the field and also notify your UI. No need to do it by hand. 
        }

        // The beacking field should not be public
        private string log = ""; // The beacking field should not be public
        
        // You really don't need this. 
        // public new event PropertyChangedEventHandler? PropertyChanged; 
    }

Read more about this here: https://www.reactiveui.net/docs/getting-started/compelling-example

Happy coding
Tim

Replies: 3 comments 6 replies

Comment options

You must be logged in to vote
1 reply
@maxkatz6
Comment options

Comment options

You must be logged in to vote
4 replies
@timunie
Comment options

@hjrb
Comment options

@timunie
Comment options

@hjrb
Comment options

Answer selected by ckorn
Comment options

You must be logged in to vote
1 reply
@timunie
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
4 participants