Link textbox to property

I can’t link my textbox to my property in C#. I have to when I write and press a button send a request, but the problem is that when my textbox is empty I can’t give it the value entered by the user.

In my view-model

class ViewModel : INotifyPropertyChanged
{
    private string textePartieA;
    public string TextePartieA
    {
        get { return textePartieA; }
        set
        {
            textePartieA = value;
            OnPropertyChanged("TextePartieA"); 
            if (!(textePartieA is null))
                TextToSpeech(apikeyTextToSpeech, urlTextToSpeech);
        }
    }

In my view:

<TextBox x:Name="TextePartieA" Margin="5" MinHeight="85" Grid.ColumnSpan="3" 
         Text="{Binding TextePartieA, Mode=TwoWay}"></TextBox>

Answer

You should bind viewModel to your DataContext too:

public class MyWindow : Window {

    public MyWindow() {

var viewModel = new ViewModel();
   
    DataContext = viewModel;
        InitializeComponents();
        
    }

   
}

Leave a Reply

Your email address will not be published. Required fields are marked *