在视图中绑定单个RIA实体

时间:2012-01-16 17:08:54

标签: c# silverlight mvvm ria prism-4

我无法理解为什么这样做(将单个Car绑定到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&lt;&gt;因为我想得到一个单一的实体。 顺便说一下,我想了解出了什么问题......

1 个答案:

答案 0 :(得分:0)

绑定到单个实体时,您应使用CurrentItem而不是ItemsSource