Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

wpf textbox text binding

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.

like image 264
alostr Avatar asked Dec 05 '12 22:12

alostr


People also ask

What is databinding in WPF?

Data binding in Windows Presentation Foundation (WPF) provides a simple and consistent way for apps to present and interact with data. Elements can be bound to data from different kinds of data sources in the form of . NET objects and XML.

What is DataContext in WPF?

The DataContext property is the default source of your bindings, unless you specifically declare another source, like we did in the previous chapter with the ElementName property. It's defined on the FrameworkElement class, which most UI controls, including the WPF Window, inherits from.

What is DataTemplate WPF?

DataTemplate is about the presentation of data and is one of the many features provided by the WPF styling and templating model. For an introduction of the WPF styling and templating model, such as how to use a Style to set properties on controls, see the Styling and Templating topic.


1 Answers

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>
like image 99
d.moncada Avatar answered Sep 22 '22 17:09

d.moncada