Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么文本块文本不在ViewModel中更新?_C#_Wpf_Xaml_Windows Phone 7_Windows Phone 8 - Fatal编程技术网

C# 为什么文本块文本不在ViewModel中更新?

C# 为什么文本块文本不在ViewModel中更新?,c#,wpf,xaml,windows-phone-7,windows-phone-8,C#,Wpf,Xaml,Windows Phone 7,Windows Phone 8,我试图通过XML序列化和隔离存储将一些值发布到下一个屏幕。值通过按钮单击事件从textbox和textBlock发布到下一屏幕的textBlock。但是TextBlock的文本不会发布到下一个屏幕,只会发布textBox文本。如果我调试并检查,XML将显示为“”,其中没有加载字符串。它也没有约束力。为什么? //MainPage.xaml <StackPanel> <Button Margin="10"

我试图通过XML序列化和隔离存储将一些值发布到下一个屏幕。值通过按钮单击事件从textbox和textBlock发布到下一屏幕的textBlock。但是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>


//SeconPage.xaml

您的绑定表达式中有一个输入错误。应该是

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

而不是

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

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

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

但在Windows Phone中,绑定仅在取消焦点控制后才起作用。您可以在此处找到讨论和一些解决方案:

试试这个。 代码片段[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);
                }
            }
   <TextBlock Content="{Binding CustomerID, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
代码段[XAML]:

   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);
                }
            }
   <TextBlock Content="{Binding CustomerID, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"

我不知道你在说什么。
TextBlock
不支持双向绑定,因为您无法通过UI直接修改值(例如在文本框中,您可以用键盘键入内容)。在PropertyChanged事件上设置断点并检查它是否为null。@HighCore那么如何通过PropertyChangedEventHandler将textBlock的内容从一个页面传输到另一个页面。我也尝试过单向绑定。@MuhammadUmar,如果为这个文本块设置了断点。它显示了我需要的价值。非常感谢。我改变了密码。现在可以了。添加第一页的xaml代码(带文本框)。我想这里也有一个输入错误。嗨,安东,现在你可以检查第一页的Xaml。你可以在第二个文本框(MyText)中键入文本,然后将焦点设置为第一个文本框,然后单击你的按钮吗。有什么不同吗?谢谢你,它现在起作用了。这是我原来的应用程序中的打字错误问题。这是样品一。谢谢你抽出时间。
   <TextBlock Content="{Binding CustomerID, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"