John

John

  • NA
  • 1
  • 1.7k

Windows Store App DataBind TextBlock to Object From Web Service

Nov 4 2012 3:24 PM

0
 


I am new to Windows Store app and I am connecting to a web service that I have created. I get an object called user that has all the information that I need to populate my controls with and the values are being populated correctly in the object. What I cannot seem to figure out is how to connect the object to the controls on the XAML page. I am currently trying to populate the users first name to the  <TextBlock x:Name="txtUserName" TextWrapping="Wrap" Text="{Binding ThisUser[0].FirstName}" VerticalAlignment="Center" FontSize="64" HorizontalAlignment="Left" Margin="5"  Width="50" Height="150"/> textblock.


I have gone through tutorials and I cannot see what I am missing.

Here is the code for my XAML:

 
 <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
        <Grid x:Name="MainMenuGrid">
            <Grid.DataContext>
                <local:CurrentUserDataSource/>
            </Grid.DataContext>
            <Grid.RowDefinitions>
                <RowDefinition Height="100"/>
                <RowDefinition Height="Auto"/>
            </Grid.RowDefinitions>
            <TextBlock TextWrapping="Wrap" Text="Welcome to Cuisinier Creatif" VerticalAlignment="Center" FontSize="64" Margin="5" HorizontalAlignment="Center"/>
            <VariableSizedWrapGrid HorizontalChildrenAlignment="Stretch" ItemHeight="195" ItemWidth="200" MaximumRowsOrColumns="2" Orientation="Vertical" Grid.Row="1" HorizontalAlignment="Center" VerticalAlignment="Center">
                <Button x:Name="btnMyRecipeBooks"  Content="My Reipie Books" Width="200" HorizontalAlignment="Stretch" Height="140" Margin="2"/>
                <Button x:Name="btnewRecipeBook"  Content="New Recipe Book" Width="200" HorizontalAlignment="Stretch" Height="140" Margin="2"/>
                <Button x:Name="btnMyRecipes"  Content="My Reipies" Width="200" HorizontalAlignment="Stretch" Height="140" Margin="2"/>
                <Button x:Name="btnNewRecipe"  Content="New Recipe" Width="200" HorizontalAlignment="Stretch" Height="140" Margin="2"/>
                <Button x:Name="btnShoppingList"  Content="Shopping List" Width="200" HorizontalAlignment="Stretch" Height="140" Margin="2"/>
                <Button x:Name="btnMyInformation"  Content="My Information" Width="200" HorizontalAlignment="Stretch" Height="140" Margin="2" />
            </VariableSizedWrapGrid>
            <TextBlock x:Name="txtUserName" TextWrapping="Wrap" Text="{Binding ThisUser[0].FirstName}" VerticalAlignment="Center" FontSize="64" HorizontalAlignment="Left" Margin="5"  Width="50" Height="150"/>
        </Grid>
    </Grid>

Code Behind For That Page:
    public sealed partial class MainMenu : Page
    {
        User usr = new User();
        public MainMenu()
        {
            this.InitializeComponent();

        }

        /// <summary>
        /// Invoked when this page is about to be displayed in a Frame.
        /// </summary>
        /// <param name="e">Event data that describes how this page was reached.  The Parameter
        /// property is typically used to configure the page.</param>
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            usr = (User)e.Parameter;
            CurrentUserDataSource cuDS = new CurrentUserDataSource(usr);
        }
    }
}

CurrentUserDataSource.cs
    class CurrentUserDataSource
    {
        public CurrentUserDataSource()
        {
        }
        public CurrentUserDataSource(User user)
        {
            LoadUser(user);
        }
        private ObservableCollection<CurrentUser> currentUser;

        public ObservableCollection<CurrentUser> ThisUser
        {
            get { return currentUser; }
            set { currentUser = value; }
        }
        private void LoadUser(User user)
        {
            currentUser = new ObservableCollection<CurrentUser>
            {
                new CurrentUser
                {
                    FirstName = user.FirstName,
                    LastName = user.LastName,
                    EmailAddress = user.LastName,
                    LastSignIn = user.LastSignIn,
                    SignUpdate = user.SignUpdate
                }
            };
        }
    }
CurrentUser.cs
 class CurrentUser:INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        private string _firstname;
        private string _lastname;
        private DateTime _lastsignin;
        private DateTime _signupdate;
        private string _emailaddress;

        public CurrentUser()
        {

        }
        public CurrentUser(User user)
        {
            this.FirstName = user.FirstName;
            this.LastName = user.LastName;
            this.LastSignIn = user.LastSignIn;
            this.SignUpdate = user.SignUpdate;
            this.EmailAddress = user.EmailAddress;
        }
        private void NotifyPropertyChanged(string property)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(property));
            }
        }
        public string FirstName
        {
            get { return this._firstname; }
            set
            {
                _firstname = value;
                NotifyPropertyChanged("FirstName");
            }
        }
        public string LastName
        {
            get { return this._lastname; }
            set
            {
                _lastname = value;
                NotifyPropertyChanged("LastName");
            }
        }
        public DateTime LastSignIn
        {
            get { return this._lastsignin; }
            set
            {
                _lastsignin = value;
                NotifyPropertyChanged("LastSignIn");
            }
        }
        public DateTime SignUpdate
        {
            get { return this._signupdate; }
            set
            {
                _signupdate = value;
                NotifyPropertyChanged("SignUpdate");
            }
        }
        public string EmailAddress
        {
            get { return this._emailaddress; }
            set
            {
                _emailaddress = value;
                NotifyPropertyChanged("EmailAddress");
            }
        }
    }