我有以下示例数据,效果很好......
<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 文件中单独创建列表?
View 模型:
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; }
}
最佳答案
在某些情况下这很容易,那些需要:
例如
public IEnumerable Employees { get; set; }
首先,您将项目提取到资源字典中,例如
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:obj="clr-namespace:Test.Objects">
<x:Array x:Key="SampleEmps" Type="obj:Employee">
<obj:Employee Name="Skeet" Occupation="Programmer" />
<obj:Employee Name="Skeet" Occupation="Programmer" />
<obj:Employee Name="Dimitrov" Occupation="Programmer" />
</x:Array>
</ResourceDictionary>
然后将其添加到
MergedDictionary
将包含 ViewModel 的控件。例如<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Objects/SampleData.xaml" />
</ResourceDictionary.MergedDictionaries>
<!-- ... -->
然后,您可以使用
StaticResource
引用该集合。 :<obj:SomeOtherViewModel Employees="{StaticResource SampleEmps}"/>
现在,专用集合的问题是您不能简单地在 XAML 中创建它们。缺少 setter 的问题是您不能使用
StaticResource
分配属性。 ,所以我认为一个二传手总是必要的。如果您有专门的收藏,您可以使用
MarkupExtension
创建一个实例。[ContentProperty("Items")]
public class GenericCollectionFactoryExtension : MarkupExtension
{
public Type Type { get; set; }
public Type T { get; set; }
public IEnumerable Items { get; set; }
public override object ProvideValue(IServiceProvider serviceProvider)
{
var genericType = Type.MakeGenericType(T);
var list = Activator.CreateInstance(genericType) as IList;
if (list == null) throw new Exception("Instance type does not implement IList");
foreach (var item in Items)
{
list.Add(item);
}
return list;
}
}
您可以直接在资源中创建例如 ObservableCollection,也可以在需要项目的地方通过此扩展管理数组:
xmlns:om="clr-namespace:System.Collections.ObjectModel;assembly=System"
<obj:SomeViewModel x:Key="SomeVM">
<obj:SomeViewModel.Employees>
<me:GenericCollectionFactory Type="{x:Type om:ObservableCollection`1}"
T="{x:Type obj:Employee}">
<StaticResource ResourceKey="SampleEmps" />
</me:GenericCollectionFactory>
</obj:SomeViewModel.Employees>
</obj:SomeViewModel>
`1
末om:ObservableCollection
是必要的,因为类型是通用的。
关于wpf - 在 Expression Blend 中重用设计数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8230455/