m-y*_*m-y 5 wpf resources xaml expression-blend sample-data
我有以下样本数据,这很好地解决了......
<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>
Run Code Online (Sandbox Code Playgroud)
但是,我发现能够重用该样本员工列表而不是每次都重新输入它是有用的.我无法弄清楚如何重用该列表.基本上,我想要另一个包含该员工列表的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>
Run Code Online (Sandbox Code Playgroud)
另外,如何在另一个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; }
}
Run Code Online (Sandbox Code Playgroud)
在某些情况下,这很容易,但需要:
例如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>
Run Code Online (Sandbox Code Playgroud)
然后将其添加到MergedDictionary将包含 ViewModel 的控件中。例如
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Objects/SampleData.xaml" />
</ResourceDictionary.MergedDictionaries>
<!-- ... -->
Run Code Online (Sandbox Code Playgroud)
然后您可以使用以下方法引用该集合StaticResource:
<obj:SomeOtherViewModel Employees="{StaticResource SampleEmps}"/>
Run Code Online (Sandbox Code Playgroud)
现在,专用集合的问题是您不能简单地在 XAML 中创建它们。缺少 setter 的问题是您无法使用 a 分配属性StaticResource,所以我认为 setter 始终是必要的。
如果您有专门的集合,则可以使用 aMarkupExtension来创建实例。
[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;
}
}
Run Code Online (Sandbox Code Playgroud)
例如,您可以直接在资源中创建 ObservableCollection,也可以通过此扩展在需要项目的位置通过管道传输数组:
xmlns:om="clr-namespace:System.Collections.ObjectModel;assembly=System"
Run Code Online (Sandbox Code Playgroud)
<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>
Run Code Online (Sandbox Code Playgroud)
末尾`1的om:ObservableCollection是必需的,因为该类型是通用的。