2012-01-16 20 views
0

綁定一個RIA實體,我不能明白爲什麼這個工程(綁定單一車到RadDataForm):在查看

XAML:

<tk:RadDataForm ItemsSource="{Binding Path=Cars}" 
    AutoGenerateFields="True" DataContext="{Binding}" /> 

視圖模型:

public void OnNavigatedTo(NavigationContext navigationContext) 
{ 
    carId = int.Parse(navigationContext.Parameters["IdRecord"]); 
    Cars= _carContext.GetCarById(carId); 
} 

private IEnumerable<Car> cars; 
public IEnumerable<Car> Cars 
{ 
    get { return this.cars; } 
    set 
    { 
     if (this.cars!= value) 
     { 
      this.cars= value; 
      this.RaisePropertyChanged(() => this.Cars); 
     } 
    } 
} 

和此不是

xaml:

<tk:RadDataForm CurrentItem="{Binding Path=CurrentCar}" 
    AutoGenerateFields="True" DataContext="{Binding}" /> 

視圖模型:

public void OnNavigatedTo(NavigationContext navigationContext) 
{ 
    carId = int.Parse(navigationContext.Parameters["IdRecord"]); 
    CurrentCar= _carContext.GetCarById(carId).FirstOrDefault(); 
} 

private Car currentCar; 
public Car CurrentCar 
{ 
    get { return this.currentCar; } 
    set 
    { 
     if (this.currentCar!= value) 
     { 
      this.currentCar= value; 
      this.RaisePropertyChanged(() => this.CurrentCar); 
     } 
    } 
} 

我不想IEnumerable的<>,因爲我想獲得一個單一的實體。 順便說一下,我想了解發生了什麼問題...

回答

0

當綁定到一個單一的實體時,您應該使用CurrentItem而不是ItemsSource

+0

謝謝,但它不起作用。我用你的建議編輯代碼。 – 2012-01-17 07:22:10

+0

您是否在VS輸出中看到任何綁定錯誤? – dmusial 2012-01-21 16:08:20

+0

我發現爲了檢索單個實體,我必須定義返回單個TEntity而不是IEnumerable 或IQueryable 的服務。這種方式可行,謝謝。 – 2012-01-30 07:18:21