2012-04-16 97 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