我有以下示例数据,效果很好...
<SampleData:DashboardViewModel xmlns:SampleData="clr-namespace:MyApp.ViewModels">
<SampleData:DashboardViewModel.Employees>
<SampleData:EmployeeViewModel FirstName="Aaron" "Adams" />
<SampleData:EmployeeViewModel FirstName="Billy" "Bob" />
<SampleData:EmployeeViewModel FirstName="Charlie" "Chaplin" />
</SampleData:DashboardViewModel.Employees>
</SampleData:DashboardViewModel>
但是,我发现能够重用该示例员工列表而不是每次都重新键入它会很有用。我不知道如何重用该列表。基本上,我想要另一个包含该员工列表的 SampleData 文件(SampleEmployees.xaml),然后能够将其包含在我的其他示例中......
<SampleData:DashboardViewModel xmlns:SampleData="clr-namespace:MyApp.ViewModels">
<SampleData:DashboardViewModel.Employees ... /> <!-- What goes in there? -->
</SampleData:DashboardViewModel>
<SampleData:OtherViewModel xmlns:SampleData="clr-namespace:MyApp.ViewModels">
<SampleData:OtherViewModel.Employees ... /> <!-- What goes in there? -->
</SampleData:OtherViewModel>
另外,如何在另一个 XAML 文件中单独创建列表?
视图模型:
public class DashboardViewModel : NotificationObject
{
public class DashboardViewModel(IDataService dataService)
{
InternalEmployees = new ObservableCollection<EmployeeViewModel>(dataService.GetEmployees());
Employees = new ReadOnlyObservableCollection<EmployeeViewModel>(InternalEmployees);
}
private ObservableCollection<EmployeeViewModel> InternalEmployees { get; set; }
public ReadOnlyObservableCollection<EmployeeViewModel> Employees { get; private set; }
}