0

I am trying to post some values to the next screen through XML serialization and Isolated Storage. Values are posted from textbox and also from textBlock to the next screen's textBlocks with button click event. But the TextBlock's text is not posted to the next screen, only the textBox text is posted. If I debug and check, XML shows as "" with no string loadedinto it. It's also not binding as well. Why?

//MainPage.xaml
        <StackPanel>
                <Button Margin="10"
                Content="Simple Sample"
                Name="btnSimple"
                Click="btnSimple_Click">                    

                </Button>
                <TextBlock TextWrapping="Wrap"   Text="{Binding FirstName, Mode=TwoWay}"/>           
                <TextBlock TextWrapping="Wrap" Text="{Binding MyText, Mode=TwoWay}"/>         

            </StackPanel>

 //MainPage.xaml.cs
        using System;
        using System.Windows;
        using Microsoft.Phone.Controls;    
        using System.IO;
        using System.IO.IsolatedStorage;
        using System.Text;
        using System.Xml.Serialization;       

        namespace WPIsolatedStorage
        {
          public partial class MainPage : PhoneApplicationPage
          {
            // Constructor
              private LastUser _User = new LastUser();
              private const string USER_KEY = "LastUser";
            public MainPage()
            {
              InitializeComponent();
            }

            private void GetUser()
            {
                string xml;

                xml = IsolatedStorageSettings.ApplicationSettings[USER_KEY].ToString();
                using (MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(xml)))
                {
                    XmlSerializer serializer = new XmlSerializer(typeof(LastUser));
                    _User = (LastUser)serializer.Deserialize(ms);
                }
            }

            private void btnSimple_Click(object sender, RoutedEventArgs e)
            {
              NavigationService.Navigate(new Uri("/PageSimple.xaml", UriKind.Relative));
            }

            private void PhoneApplicationPage_Loaded_1(object sender, RoutedEventArgs e)
            {
                if (IsolatedStorageSettings.ApplicationSettings.Contains(USER_KEY))
                    GetUser();

                this.DataContext = _User;            

            }
          }
        }

 //LastUser.cs
    using System.ComponentModel;

    namespace WPIsolatedStorage
    {       

      public class LastUser : INotifyPropertyChanged
      {
        #region INotifyPropertyChanged Event   
        public event PropertyChangedEventHandler PropertyChanged;   
        protected void RaisePropertyChanged(string propertyName)
        {
          if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }   
        #endregion

        #region Private Variables
        private string mFirstName = string.Empty;

        private string mTextBlock = string.Empty;
        #endregion

        #region Public Variables
        public string FirstName
        {
          get { return mFirstName; }
          set
          {
            if (mFirstName != value)
            {
              mFirstName = value;
              RaisePropertyChanged("FirstName");
            }
          }
        }

        public string MyText
    {
        get { return this.mTextBlock; }
        set
        {
            this.mTextBlock = value;
            this.RaisePropertyChanged("MyText");

        }
    }}
    } 

//SeconPage.xaml

 <TextBox Grid.Column="1"
               Grid.Row="0"
               Text="{Binding FirstName, Mode=TwoWay}"
               Name="txtFirstName" />

<TextBlock Grid.Column="1" Name="MyTextBlock" HorizontalAlignment="Left" Margin="12,178,0,-224" Grid.Row="4" TextWrapping="Wrap" Text="{Binding MyText, Mode=TwoWay}" VerticalAlignment="Top" Height="93" Width="191" FontSize="36"/>
madhu kumar
  • 786
  • 1
  • 12
  • 46
  • 1
    I have no idea what you're talking about. A `TextBlock` doesn't support Two Way binding because there's no way for you to modify the value via the UI directly (such as in the case of a TextBox, where you can type stuff with the keyboard). – Federico Berasategui Sep 27 '13 at 15:20
  • 1
    Set break point on PropertyChanged event and check either it is null or not. – Muhammad Umar Sep 27 '13 at 15:22
  • @HighCore then how can I transfer the content of textBlock from one page to another page through PropertyChangedEventHandler. I tried with OneWay Binding also. – madhu kumar Sep 27 '13 at 15:25
  • @MuhammadUmar If break point is set for this textblock. it's showing the value which I need. thank you. I've changed code. It's working now. – madhu kumar Sep 27 '13 at 16:14

2 Answers2

1

There is a typo in you binding expression. Should be

<TextBlock Grid.Column="1" .... Text="{Binding MyText}" ../>

instead of

<TextBlock Grid.Column="1" .... Text="{Binding Mytext}" ../>

My second assumption is: you use a simple binding in your code

<TextBlock TextWrapping="Wrap" Text="{Binding MyText, Mode=TwoWay}"/>

But in Windows Phone the binding works only after you unfocused control. You can find a discussion and few solutions here:

TextBox Binding TwoWay Doesn't Update Until Focus Lost WP7

Community
  • 1
  • 1
Anton Sizikov
  • 9,105
  • 1
  • 28
  • 39
1

Try This. Code snippet[C#]:

   public class Data : INotifyPropertyChanged
        {
            private int customerID;

            public int CustomerID
            {
                get { return customerID; }
                set { customerID = value; OnPropertyChanged("CustomerID"); }
            }
      public event PropertyChangedEventHandler PropertyChanged;

            public void OnPropertyChanged(string propertyName)
            {
                PropertyChangedEventHandler handler = PropertyChanged;
                if (handler != null)
                {
                    var e = new PropertyChangedEventArgs(propertyName);
                    handler(this, e);
                }
            }

Code snippet[XAML]:

   <TextBlock Content="{Binding CustomerID, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
Check Mate
  • 107
  • 2
  • 10