0
votes

first of all sorry for my english.

I am developing a WP7 app, and I still haven't completely understood the data binding structure. I have a page that has some data obtained through data binding. Data is generated within the .cs, and it works fine.

But on another page I have some data that is obtained from data binding too, but I want it to come from a UI input text instead. It's simple, just a textbox and a textblock, so the user writes something on the textbox and so it shows up on the textblock that's on the same page. But it's not working, the textblock remains empty.

It's something like this:

<TextBox Name="TestInput">
<TextBlock Text="{Binding TestText}">

Above is what's on the XAML.

public partial class NewItem : PhoneApplicationPage
{
    public String TestText { get; set; }

    public NewItem()
    {
        InitializeComponent();
        TestText = "TestInput.Text";
    }
}

And this above is what's on the C#.

BUT!! It doesn't end here. Since the textblock wasn't showing anything, I ended desperately trying to assign some plain String to the TestText property. Like this:

TestText = "HELLO WORLD";

But when the app starts and the page loads, the textblock shows nothing. I just don't understand what am I missing, or doing wrong.

I will appreciate if someone could clear me up the databinding structure, or at least explain me what did I do wrong so I can figure it out myself.

Thanks in advance guys!

2

2 Answers

0
votes

You have to assign the DataContext before you want the binding effect.So whenever there is a change in the text box, you write the code in the textchanged event.

this.DataContext=TestText

And one more change that you need to perform is that you are not actually setting the property.It should be like

TestText=TestInput.Text

0
votes

for your understanding of binding i am putting a simple working example..just follow this.. this is you page on which you bind the data of textbox to someproperty textboxText..when you finished writing in this textbox.then all the writtentext automatically come ino this property. and this property is also binded to textbloack so when your textbloack got focus it will got to the get of the property and automatically fill it.

<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
    <StackPanel Orientation="Horizontal" >
        <TextBox x:Name="testTextbox" Height="50" Width="200" Text="{Binding TextboxText,Mode=TwoWay}" />
        <TextBlock  x:Name="testTextblock" Height="50" Width="1000" Text="{Binding TextboxText,Mode=OneWay}" Foreground="White" />
    </StackPanel>
</Grid>

this is your page.cs class in which i have also shown you how to implement inotifyproperty changed..it will help you in future..

public sealed partial class MainPage : Page,INotifyPropertyChanged
{
    public MainPage()
    {
        this.InitializeComponent();
        this.DataContext = this;
    }

    private string _TextboxText;
    public string TextboxText
    {
        get
        {
            return _TextboxText;
        }
        set
        {
            _TextboxText = value;

            FirePropertyChanged("TextboxText");
            testTextblock.UpdateLayout();

        }
    }


    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
    }

    public event PropertyChangedEventHandler PropertyChanged;
    protected void FirePropertyChanged(string propertyName)
    {
        if (this.PropertyChanged != null)
        {
            this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}