【发布时间】: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