【问题标题】:Windows Phone App: Binding to the property of pageWindows Phone App:绑定到页面的属性
【发布时间】:2015-03-07 18:27:21
【问题描述】:

我制作了一个简单的应用程序来研究绑定过程。有我的代码:

MainPage.xaml

<phone:PhoneApplicationPage
    x:Class="PhoneApp1.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    DataContext="{Binding RelativeSource={RelativeSource Self}}"
    x:Name="thisPage">

    <StackPanel x:Name="LayoutRoot" Background="Transparent">
        <TextBlock
            Text="{Binding Path=TestText}"/>
        <TextBlock
            Text="Saparator"/>
        <TextBlock
            Text="{Binding ElementName=thisPage, Path=DataContext.TestText}"/>
    </StackPanel>

</phone:PhoneApplicationPage>

MainPage.xaml.cs

using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using PhoneApp1.Resources;

namespace PhoneApp1
{
    public partial class MainPage : PhoneApplicationPage
    {
        public string TestText;

        public MainPage()
        {
            InitializeComponent();

            TestText = "It works!";
        }
    }
}

如您所见,我尝试了两种方法将 TextBlock 控件的 Text 属性绑定到 MainPage 的属性。当我尝试运行此应用程序时,我在第一个 TextBlock 和第三个 TextBlock 中都看不到任何文字。

我做错了什么?

谢谢!

【问题讨论】:

    标签: c# xaml windows-phone-8 binding


    【解决方案1】:

    您尝试将字段public string TestText; 替换为属性public string TestText {get;set;}

    【讨论】:

      【解决方案2】:

      绑定仅适用于public string TestText { get; set; } 等公共属性 但仅添加不会有帮助,您的MainPage 将必须实现INotifyPropertyChanged 接口(@98​​7654321@),您必须将您的公共属性更改为:

      private string _textBackingField;
      
      public string TestText
      { 
          get
          {
              return _textBackingField;
          }
          set
          {
              _textBackingField = value;
              NotifyPropertyChanged();
          }
      }
      

      【讨论】:

      • 你说得对。如果我在加载页面后尝试设置 TestText 它不起作用。好的,我会按照你描述的方式尝试解决这个问题。
      • 我做了以下更改:公共部分类 MainPage:PhoneApplicationPage、INotifyPropertyChanged 并添加:公共事件 PropertyChangedEventHandler PropertyChanged; public void NotifyPropertyChanged(String propertyName) { PropertyChangedEventHandler handler = PropertyChanged; if (null != handler) { handler(this, new PropertyChangedEventArgs(propertyName)); } } 现在它可以正常工作了!非常感谢!
      猜你喜欢
      • 2014-07-03
      • 1970-01-01
      • 2014-10-30
      • 1970-01-01
      • 2015-03-29
      • 1970-01-01
      • 1970-01-01
      • 2015-06-08
      • 1970-01-01
      相关资源
      最近更新 更多