Use design data in Expression Blend?

I have the following data examples that work well ...

<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>

However, I believe that it would be useful to be able to reuse this list of exemplary employees, rather than repeating them each time. I cannot figure out how to reuse this list. Basically, I want to have another SampleData file (SampleEmployees.xaml) that contains this list of employees, and then include this in my other samples ...

<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>

Also, how to create a list separately in another XAML file ??

ViewModel:

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; }
}
+5
source share
1 answer

In some cases, this is easy; they require:

  • IEnumerable IList ( )
  • .

. 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 , .

0

All Articles