【发布时间】:2011-01-18 08:16:27
【问题描述】:
我正在使用 Mvvm-Light 并且一直忽略 XAML 中的绑定是如何真正起作用的。
这是我的 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 绑定到代码隐藏中的本地属性”。
我仍然不明白下面提供的两种解决方案。所以下面还有更多问题......
【问题讨论】:
标签: c# silverlight xaml windows-phone-7 mvvm-light