In some cases, this is easy; they require:
. 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 , ViewModels. .
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Objects/SampleData.xaml" />
</ResourceDictionary.MergedDictionaries>
StaticResource:
<obj:SomeOtherViewModel Employees="{StaticResource SampleEmps}"/>
, XAML. , 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 , .