wpf - 在 Expression Blend 中重用设计数据?

标签 wpf xaml resources 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>

但是,我发现能够重复使用该样本员工列表而不是每次都重新键入它会很有用。我不知道如何重用该列表。基本上,我想要另一个包含该员工列表的 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; }
}

最佳答案

在某些情况下这很容易,那些需要:

  • 集合属性的类型是 IEnumerable 或 IList(不是实现它的类)
  • 该属性(property)有一个二传手。

  • 例如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>
    
    `1om:ObservableCollection是必要的,因为类型是通用的。

    关于wpf - 在 Expression Blend 中重用设计数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8230455/

    相关文章:

    ruby-on-rails - 嵌套资源 - 如何避免冗余路由?

    .net - 在 MVVM 中,DataTemplates 是否将 Views 视为 UserControls 是 Views?

    c# - WPF slider thumb.DragCompleted

    wpf - 将项目添加到 DataGrid (MVVM)

    WPF - 选项卡中的 xaml 滚动条

    wpf - 数据模板的复用

    .net - 是否有 WPF 控件可用于展开/折叠面板(动画)

    VSTS : Different checksum values given for '*.g.cs' Files 的 WPF 警告

    c# - 如何制作带注释的滚动条?

    java - getResources().getString() 没有为我的类定义