【问题标题】:Why TextBlock Text is not updating in ViewModel?为什么 TextBlock Text 在 ViewModel 中没有更新?
【发布时间】:2013-09-27 15:14:42
【问题描述】:

我正在尝试通过 XML 序列化和独立存储将一些值发布到下一个屏幕。值通过按钮单击事件从文本框和文本块发布到下一个屏幕的文本块。但是 TextBlock 的文本不会发布到下一个屏幕,只会发布 textBox 文本。如果我调试和检查,XML 显示为“”,其中没有加载任何字符串。它也没有约束力。为什么?

//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"/>

【问题讨论】:

  • 我不知道你在说什么。 TextBlock 不支持双向绑定,因为您无法直接通过 UI 修改值(例如在 TextBox 的情况下,您可以使用键盘输入内容)。
  • 在 PropertyChanged 事件上设置断点并检查它是否为空。
  • @HighCore 那么如何通过 PropertyChangedEventHandler 将 textBlock 的内容从一个页面传输到另一个页面。我也尝试过使用 OneWay Binding。
  • @MuhammadUmar 如果为此文本块设置断点。它显示了我需要的价值。谢谢。我已经更改了代码。它现在正在工作。

标签: c# wpf xaml windows-phone-7 windows-phone-8


【解决方案1】:

您的绑定表达式中有错字。应该是

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

而不是

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

我的第二个假设是:您在代码中使用了简单的绑定

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

但在 Windows Phone 中,绑定仅在您没有集中控制之后才起作用。您可以在这里找到讨论和一些解决方案:

TextBox Binding TwoWay Doesn't Update Until Focus Lost WP7

【讨论】:

  • 添加第一页的 xaml 代码(带文本框)。我认为那里也有一个错字。
  • 嗨 Anton,现在你可以查看首页 Xaml。
  • 您能否在第二个文本框(MyText)中输入文本,然后将焦点设置到第一个文本框,然后单击您的按钮。有什么区别吗?
  • 谢谢你。它正在工作。这是我原始应用程序中拼写错误的问题。这是样本一。谢谢你的时间。
【解决方案2】:

试试这个。 代码 sn-p[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);
                }
            }

代码 sn-p[XAML]:

   <TextBlock Content="{Binding CustomerID, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"

【讨论】:

    猜你喜欢
    • 2012-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-28
    • 2020-02-02
    • 2021-06-22
    相关资源
    最近更新 更多