2012-04-16 103 views
1

我正在使用MVVM light與EF4和SQL CE 4一起使用,但我遇到了可觀察集合的問題。我的應用程序不一定需要使用mvvm模式,但由於我需要observablecollection的好處,因此我決定學習如何將它集成。我可以成功地將我的數據庫屬性實體鏈接到我的列表框中並顯示它們,我也可以將這些實體的一些屬性鏈接到文本框,但是當我嘗試通過在文本框中鍵入來更新這些屬性時,我被卡住了。這裏是我的一個文本框和列表框XAML代碼:MVVM Light綁定到Observable Collection

<TextBox Text="{Binding SaleTitle, ValidatesOnDataErrors=true, Mode=TwoWay}" 
    <ListBox Height="424" 
     Margin="24,80,0,0"    
     x:Name="listBoxProperties" 
     VerticalAlignment="Top" 
     ItemTemplate="{StaticResource propertySummaryTemplate}" 
     IsSynchronizedWithCurrentItem="True" 
     Width="216" BorderThickness="0" Background="{x:Null}" 
     FontFamily="Segoe UI" 
     ItemsSource="{Binding PropertyList}" 
     SelectedItem="{Binding CurrentProperty, Mode=TwoWay}" 
     ScrollViewer.HorizontalScrollBarVisibility="Disabled" 
     UseLayoutRounding="True" 
     HorizontalAlignment="Left" 
     ScrollViewer.VerticalScrollBarVisibility="Disabled" >   
    </ListBox> 

這裏是我的MainViewModel的部分代碼:

private string _SaleTitle; 
    public string SaleTitle 
    { 
     get 
     { 
      if (CurrentProperty != null) 
       return CurrentProperty.SaleTitle; 
      else 
       return ""; 
     } 
     set 
     { 
      _SaleTitle = value; 
      RaisePropertyChanged("SaleTitle"); 
     } 
    } 



       private RelayCommand loadCommand; 
    public ICommand LoadCommand 
    { 
     get 
     { 
      if (loadCommand == null) 
       loadCommand = new RelayCommand(() => Load()); 
      return loadCommand; 
     } 
    } 
    private void Load() 
    { 
     PropertyList = new ObservableCollection<Property>((from property in entities.Properties.Include("Images") 
                  select property)); 
     propertyView = CollectionViewSource.GetDefaultView(PropertyList); 
     if (propertyView != null) 
      propertyView.CurrentChanged += new System.EventHandler(propertyView_CurrentChanged); 
     RaisePropertyChanged("CurrentContact"); 
     RaisePropertyChanged("SaleTitle"); 
     RaisePropertyChanged("Address"); 
     RaisePropertyChanged("AuctioneerName"); 
     RaisePropertyChanged("AgentName"); 
     RaisePropertyChanged("Price"); 
     RaisePropertyChanged("NextBid"); 
     RaisePropertyChanged("Status"); 
    } 


     void propertyView_CurrentChanged(object sender, System.EventArgs e) 
    { 
     RaisePropertyChanged("CurrentContact"); 
     RaisePropertyChanged("SaleTitle"); 
     RaisePropertyChanged("Address"); 
     RaisePropertyChanged("AuctioneerName"); 
     RaisePropertyChanged("AgentName"); 
     RaisePropertyChanged("Price"); 
     RaisePropertyChanged("NextBid"); 
     RaisePropertyChanged("Status"); 
    } 

    private Property _CurrentProperty; 
    public Property CurrentProperty 
    { 
     get 
     { 
      if (propertyView != null) 
       return propertyView.CurrentItem as Property; 
      return null; 
     } 

     set 
     { 
      _CurrentProperty = value; 
      RaisePropertyChanged("CurrentProperty"); 
     } 
    } 


    public ObservableCollection<Property> PropertyList 
    { 
     get 
     { 
      return propertyList; 
     } 

     set 
     { 
      if (propertyList == value) 
      { 
       return; 
      } 

      var oldValue = propertyList; 
      propertyList = value; 

      // Update bindings, no broadcast 
      RaisePropertyChanged(PropertiesPropertyName); 
     } 
    } 

    public MainViewModel() 
    { 
     if (IsInDesignMode) 
     { 
      // Code runs in Blend --> create design time data. 
     } 
     else 
     { 
      // Code runs "for real" 
      entities = new Model1Container1(); 
     } 
    } 

    ////public override void Cleanup() 
    ////{ 
    //// // Clean up if needed 

    //// base.Cleanup(); 
    ////} 
} 

}

列表框與從內容成功地填充當前選定的項目,但是當我輸入它並單擊它或做任何事情以失去焦點時,它就會回到之前的狀態。

回答

2

看看你的SaleTitle屬性定義。它從CurrentProperty.Saletitle讀取值,但將值設置爲本地字段,而不是任何其他字段。

+0

我發誓我已經試過了,不敢相信它那麼簡單 - 一定是我的腦子在連續10個小時後玩弄技巧! – randomalbumtitle 2012-04-16 07:24:30