好的相当简单,我的页面上有一个 ToggleSwitch,就像这样。
<toolkit:ToggleSwitch x:Name="togLocation" VerticalAlignment="Center"
Style="{StaticResource gToggleSwitchStyle}" Foreground="{StaticResource brLightFont}"
Loaded="togLocation_Loaded" IsChecked="{Binding LocationTrackingEnabled, Mode=TwoWay}"/>
在同一页面后面的代码中,我有以下内容
public partial class Setup : PhoneApplicationPage
{
public bool LocationTrackingEnabled
{
get { return (bool)GetValue(LocationTrackingEnabledProperty); }
set
{
SetValue(LocationTrackingEnabledProperty, value);
}
}
public static readonly DependencyProperty LocationTrackingEnabledProperty =
DependencyProperty.Register("LocationTrackingEnabled",
typeof(bool),
typeof(Setup),
new PropertyMetadata(false));
// blah blah rest of the page code follows on
如您所见,默认值为 false,因此 toggleswitch.IsChecked 应为 false。不,它是真的。使用断点我检查 LocationTrackingEnabled 并且它永远不会改变,所以绑定不起作用。
我已经在后面的代码中尝试过了。
DataContext = this;
我也在页面的 xaml 中尝试过这个
DataContext="{Binding RelativeSource={RelativeSource Self}}"
我以前做过绑定,但我看不到问题。