简体   繁体   English

数据绑定不起作用,怎么了? 银光WP7

[英]Data binding not working, What is wrong? Silverlight WP7

I'm new to Silverlight and i'm trying to use Databinding. 我是Silverlight的新手,正在尝试使用数据绑定。 This looks simple but it's not working and I can't find why... 这看起来很简单,但无法正常工作,我找不到原因...

In my MainPage.xaml 在我的MainPage.xaml中

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

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

         CredentialsProvider="{StaticResource BingMapsKey}" />

As you can see, I'm attempting a binding on ZoomLevel and Center. 如您所见,我正在尝试对ZoomLevel和Center进行绑定。

In my MainPage.xaml.cs 在我的MainPage.xaml.cs中

The class inherit from INotifyPropertyChanged 该类继承自INotifyPropertyChanged

In the constructor: 在构造函数中:

ZoomLevel = 12.0;
Center = new GeoCoordinate(0, 0);

The properties: 属性:

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));
}

I'm I forgetting something? 我忘记了什么?

I have stuck on this for 3 hours starting to be a while for a simple binding... 我已经坚持了3个小时,开始需要一段时间才能进行简单的绑定...

Thank you in advance for your help! 预先感谢您的帮助! :) :)

Try changing the properties to public: 尝试将属性更改为public:

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"); }
}

And also set the View DataContext: (as Ray mentioned in his answer) 并设置View DataContext :(如Ray在他的回答中所述)

public partial class MainPage
{
    public MainPage()
    {
        this.DataContext = this;
    }
}

It is highly recommended to use the MVVM pattern. 强烈建议使用MVVM模式。

In addition to the properties needing to be public (as per MichaelS's answer), bindings reference the object that is set to the control's DataContext (or its parent's DataContext). 除了需要公开的属性(按照MichaelS的回答)之外,绑定还引用设置为控件的DataContext(或其父级的DataContext)的对象。

So typically you wouldn't have your Window implement INotifyPropertyChanged but you would create another class (normally called a ViewModel) that implements INotifyPropertyChanged and set that to the Window's DataContext . 所以通常你不会有你的窗口实现INotifyPropertyChanged ,但你会创建另一个类(通常称为视图模型),它实现INotifyPropertyChanged ,并设置为窗口的DataContext

eg 例如

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));
    }
}

Then in your MainPage.xaml.cs you could do something like this 然后在MainPage.xaml.cs中,您可以执行以下操作

public partial class MainPage
{
    public MainPage(MainWindowViewModel vm)
    {
        this.DataContext = vm;
    }
}

Of course, a quick fix for you might be to just set your DataContext for the page to be itself. 当然,对您来说,一个快速的解决方法可能就是将您的DataContext设置为页面本身。

eg 例如

public partial class MainPage
{
    public MainPage()
    {
        this.DataContext = this;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM