xaml双向绑定到Phone Application Page代码后面的属性

时间:2012-05-16 14:40:08

标签: c# windows-phone-7 xaml

确定相当简单,我的页面上有一个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}}"

之前我已经完成了绑定,但我看不出这个问题。

1 个答案:

答案 0 :(得分:0)

这不需要是DependencyProperty。只需将其用作常规财产即可。此外,运行时检查调试输出 - 如果遇到绑定问题,它将显示在那里。

相关问题