2011-10-04 76 views
0

我是新來的Silverlight,我試圖使用數據綁定。 這看起來簡單,但它不工作,我找不到爲什麼...數據綁定不起作用,出了什麼問題? Silverlight WP7

在我MainPage.xaml中

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

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

     CredentialsProvider="{StaticResource BingMapsKey}" /> 

正如你所看到的,我試圖在縮放級別和綁定中央。

在我MainPage.xaml.cs中

類繼承INotifyPropertyChanged的

在構造函數中:

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

性能:

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小時開始是一個簡單的綁定一段時間...

預先感謝您的幫助! :)

回答

4

嘗試改變屬性市民:

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:

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

強烈建議使用MVVM(如雷他的回答中提到)模式。

+0

嗯,我希望這麼辛苦,這是去上班......但是事與願違:/還是要謝謝你。 –

+1

@尼克勞斯:雷是正確的,你也應該設置Datacontext。建議使用MVVM模式。我也編輯了答案。 – MichaelS

+0

非常感謝!我在某處讀到數據上下文自動設置爲根目錄...但它看起來像是一個謊言(如蛋糕)。 –

3

除了需要公開的屬性外(根據MichaelS的回答),綁定引用了設置爲控件的DataContext(或其父代的DataContext)的對象。

因此,您通常不會讓您的窗口實現INotifyPropertyChanged,但您會創建另一個類(通常稱爲ViewModel),該類實現INotifyPropertyChanged並將其設置爲窗口的DataContext

例如

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

然後在你的MainPage.xaml.cs中,你可以做這樣的事情

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

當然,一個速戰速決你可能只設置你的DataContext的頁面是本身。

例如

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

謝謝你的好建議和想法。我投了你的答案:) –

+0

而且我將繼續使用ViewModel的方式;) –