0

Hello guys I recently got started with windows store apps development and right now I'm studying data binding. I looked up at the MSDN for examples and tried running it but I keep getting an error this is the code I'm working on

This is my Mainpage.xaml file

<Grid x:Name="LayoutRoot" Background="#FF0C0C0C">
    <TextBox x:Name="textBox1" Text="{Binding}" FontSize="30"
    Height="120" Width="440" IsReadOnly="True"
    TextWrapping="Wrap" AcceptsReturn="True" />
</Grid>

This is Mainpage.xaml.cs file

// Constructor
public MainPage()
{
    InitializeComponent();

    // Set the data context to a new Recording.
    textBox1.DataContext = new Recording("Chris Sells", "Chris Sells Live", 
        new DateTime(2008, 2, 5));
    // Theres an error message that says textBox1 does not appear in the current context though i have declared it in the MainPage.xaml file


}

// A simple business object
public class Recording
{
    public Recording() { }

    public Recording(string artistName, string cdName, DateTime release)
    {
        Artist = artistName;
        Name = cdName;
        ReleaseDate = release;
    }

    public string Artist { get; set; }
    public string Name { get; set; }
    public DateTime ReleaseDate { get; set; }

    // Override the ToString method.
    public override string ToString()
    {
        return Name + " by " + Artist + ", Released: " + ReleaseDate.ToString("d");
    }
}

I keep getting an error message at my MainPage.xaml.cs file that:

textbox1 doesnt exits

although it is in the MainPage.xaml file.

2
  • I have added a name to the textbox and build the solution but i keep getting this error message at my MainPage.xaml.cs " textBox1 does not exists in the current context " I was wondering if there is way to make my cs file recognize the textbox. Im using virtual studio 2013 by the way. Commented Jul 26, 2015 at 16:52
  • Did you check the below answers after you have posted this question ?? Let us know if this answers worked for you? Commented Dec 12, 2015 at 18:01

1 Answer 1

1

I am not sure why compiler says you this error. Because name looks OK in your code. From my experience it could be error in other place and compiler says one error message for all kind of errors (I had such situation with first version of SDK for Windows Phone).

Next thins don't looks good as for me:

  • You bound Text property of TextBox to data context. But on other hand you set object as data context in constructor. It is a bit hard to bind to object (usual you receive result of ToString() method in WPF). It could reason of error. Try to bind to certain property. For example: Text="{Binding Name}"
  • DataContext is inherit property. It means if you set DataContext for window all property of controls inside of this window will be same. So best practice for this is replace setting code to next this.DataContext=..
  • When you update properties of Recording class, UI will know nothing about this. You have to implement INotifyPropertyChanged interface for fixing this.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.