C# 数据绑定不工作,怎么了?银光WP7

C# 数据绑定不工作,怎么了?银光WP7,c#,silverlight,data-binding,windows-phone-7,C#,Silverlight,Data Binding,Windows Phone 7,我是Silverlight的新手,我正在尝试使用数据绑定。 这看起来很简单,但它不起作用,我不知道为什么 在myMainPage.xaml中 <map:Map Name="bing_map" Height="578" Width="480" ZoomLevel="{Binding ZoomLevel, Mode=TwoWay}" Center="{Binding Center, Mode=TwoWay}" CredentialsPr

我是Silverlight的新手,我正在尝试使用数据绑定。 这看起来很简单,但它不起作用,我不知道为什么

在myMainPage.xaml中

<map:Map Name="bing_map" Height="578" Width="480"

         ZoomLevel="{Binding ZoomLevel, Mode=TwoWay}"
         Center="{Binding Center, Mode=TwoWay}"

         CredentialsProvider="{StaticResource BingMapsKey}" />
特性:

private double _zoom_level;
private double ZoomLevel
{
    get { return _zoom_level; }
    set {
        if (_zoom_level == value) return;
        _zoom_level = value;
        RaisePropertyChanged("ZoomLevel");}
}

private GeoCoordinate _center;
private GeoCoordinate Center
{
    get { return _center; }
    set {
        if (_center == value) return;
        _center = value;
        RaisePropertyChanged("Center"); }
}

public event PropertyChangedEventHandler PropertyChanged;
void RaisePropertyChanged(string propertyName)
{
    var handler = PropertyChanged;
    if (handler != null)
        handler(this, new PropertyChangedEventArgs(propertyName));
}
我忘了什么吗?

我已经坚持了3个小时,开始是一个简单的绑定一段时间


提前感谢您的帮助!:)

尝试将属性更改为公共:

private double _zoom_level;
public double ZoomLevel
{
    get { return _zoom_level; }
    set {
        if (_zoom_level == value) return;
        _zoom_level = value;
        RaisePropertyChanged("ZoomLevel");}
}

private GeoCoordinate _center;
public GeoCoordinate Center
{
    get { return _center; }
    set {
        if (_center == value) return;
        _center = value;
        RaisePropertyChanged("Center"); }
}
并设置视图DataContext:(正如Ray在回答中提到的)


强烈建议使用该模式

除了需要公开的属性(根据MichaelS的回答),绑定还引用设置为控件的DataContext(或其父对象的DataContext)的对象

因此,通常您不会让窗口实现
INotifyPropertyChanged
,而是创建另一个类(通常称为ViewModel),该类实现
INotifyPropertyChanged
,并将其设置为窗口的
DataContext

e、 g

然后在MainPage.xaml.cs中,您可以执行类似的操作

public partial class MainPage
{
    public MainPage(MainWindowViewModel vm)
    {
        this.DataContext = vm;
    }
}
当然,对您来说,一个快速解决方法可能是将页面的DataContext设置为自身

e、 g


嗯,我是如此的希望它能起作用。。。但它没有:/无论如何谢谢。@Niklaos:Ray是对的,您还应该设置Datacontext。建议使用MVVM模式。我也编辑了答案。谢谢,就是这样!我在某个地方读到数据上下文在根目录上自动设置为这个。。。但这看起来像是个谎言(就像蛋糕一样)。谢谢你的建议和想法。我对你的答案投了赞成票:)我还是选择ViewModel方式;)
public partial class MainPage
{
    public MainPage()
    {
        this.DataContext = this;
    }
}
public class MainWindowViewModel : INotifyPropertyChanged
{
    private GeoCoordinate _center;
    public GeoCoordinate Center
    {
        get { return _center; }
        set 
        {
             if (_center == value) return;
             _center = value;
            RaisePropertyChanged("Center"); }
        }

    public event PropertyChangedEventHandler PropertyChanged;
    void RaisePropertyChanged(string propertyName)
    {
        var handler = PropertyChanged;
        if (handler != null)
            handler(this, new PropertyChangedEventArgs(propertyName));
    }
}
public partial class MainPage
{
    public MainPage(MainWindowViewModel vm)
    {
        this.DataContext = vm;
    }
}
public partial class MainPage
{
    public MainPage()
    {
        this.DataContext = this;
    }
}