我是使用Mvvm-Light框架的,一直以来都忽略了XAML中绑定的真正工作方式。以下是我的XAML代码:
我在这里找到了一个提示:http://bursjootech.blogspot.com/2007/12/bind-from-xaml-to-local-property-in.html 他的问题不同:如何“从XAML绑定到后台代码中的本地属性”。
我仍然不理解下面提供的两个解决方案。因此还有更多问题...
<phone:PhoneApplicationPage.Resources>
</phone:PhoneApplicationPage.Resources>
<Grid x:Name="LayoutRoot" Background="Transparent">
<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,14">
<TextBlock x:Name="ApplicationTitle" Text="{Binding SecuritySystemName}" Style="{StaticResource PhoneTextNormalStyle}"/>
<TextBlock x:Name="PageTitle" Text="{Binding PageName}" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
</StackPanel>
<TextBlock Name="textCode"
DataContext="{WHAT GOES HERE to bind to properties on my View (SecurityPanelPage class)}"
Text="{Binding Path=Code}" />
</Grid>
{Binding SecuritySystemName} 和 {Binding PageName} 正确绑定到我的ViewModel(SecuirtyPanelViewModel)。但我希望TextBlock元素中的{Binding Code}绑定到我的VIEW(而不是ViewModel)。
我已经搜索了很多文档和示例,解释了DataContext和Binding支持的语法和值。但没有什么有用的信息。
我想知道的是如何设置DataContext(或在{Binding ...}中指定某些内容),以便将其指向我的View对象。我尝试过“Self”和各种“RelativeSource”方法,但都没有成功。猜测并不是有效的方法,因为在XAML被解析之前进入调试器需要太长时间。
谢谢。
更新 - 我找到了一个答案,让我可以继续前进,但我仍然不理解,所以我有跟进问题要问下面的优秀发帖者...
以下是有效的内容:
<phone:PhoneApplicationPage x:Name="ThisPage">
<TextBlock Name="textCode" Text="{Binding Code, ElementName=ThisPage"/>
</phone:PhoneApplicationPage>
我在这里找到了一个提示:http://bursjootech.blogspot.com/2007/12/bind-from-xaml-to-local-property-in.html 他的问题不同:如何“从XAML绑定到后台代码中的本地属性”。
我仍然不理解下面提供的两个解决方案。因此还有更多问题...