wpf textbox text binding

15,701

The string passed into the NotifyPropertyChanged event should be the same name of the property itself.

public string songsFolder 
{ 
    get 
    { 
      return folder; 
    } 
    set 
    { 
      folder = value; 
      NotifyPropertyChanged("songsFolder"); 
    }
}

Also,

try adding UpdateSourceTrigger="PropertyChanged" to the binding of the textBox

<TextBox Name="browseBox" Text="{Binding Source={StaticResource ResourceKey=song}, Path=songsFolder, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Grid.Column="0"></TextBox>

Edit: Maybe the DataContext is not getting set correctly. You can also try this method (W/out a static Key)

Code behind, inside the Ctor of the window:

browseBox.DataContext = new song();

Then, update textBox finding to:

<TextBox Name="browseBox" Text="{Binding Path=songsFolder, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Grid.Column="0"></TextBox>
Share:
15,701
alostr
Author by

alostr

Updated on June 18, 2022

Comments

  • alostr
    alostr almost 2 years

    i am trying to bind the text of a textbox to a property in my class, and it is not working, I am editing the property in the code behind but I don't see the string in the textbox this is the class, and the property i am trying to bind is called songFolder.

    public class song :  INotifyPropertyChanged
    {
        public string title {get; set; }
        public string artist { get; set; }
        public string path { get; set; }
        public static string folder;
        public string songsFolder { get { return folder; } set { folder = value; NotifyPropertyChanged("songsFolder"); } }
    
        public event PropertyChangedEventHandler PropertyChanged;
    
        private void NotifyPropertyChanged(String propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    
        public song()
        {
    
        }
    
        public song(string title, string artist, string path)
        {
            this.title = title;
            this.artist = artist;
            this.path = path;
        }
    
    }
    

    and the xaml, containing the resource and the textbox wich i am tring to bind

    <Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication1"
        Title="Song Filler" Height="455" Width="525">
    <Window.Resources>
        <local:song x:Key="song"/>
    </Window.Resources>
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="auto"/>
            </Grid.ColumnDefinitions>
            <TextBox Name="browseBox" Text="{Binding Source={StaticResource ResourceKey=song}, Path=songsFolder, Mode=TwoWay}" Grid.Column="0"></TextBox>
            <Button Grid.Column="1" Width="auto" Click="Browse">browse</Button>
        </Grid>
    

    --------------update---------------- I added the next line to ctor of the window:

    BrowseBox.DataContext=new song()
    

    And while debugging I saw that the property is changing but the text in the textbox isn't.