I want to instantiate objects in XAML, and reuse these instances. I think it should be simple but I'm stuck, I'm probably missing something obvious.
Say I want to add Cats to different Rooms (Room has an ObservableCollection containing objects of type Cat). In the UserControl.Resources I create ObjectDataProviders:
<ObjectDataProvider x:Key="Cat1" ObjectType="{x:Type local:Cat}">
<ObjectDataProvider.ConstructorParameters>
<System:String>Tom</System:String>
</ObjectDataProvider.ConstructorParameters>
</ObjectDataProvider>
<ObjectDataProvider x:Key="Cat2" ObjectType="{x:Type local:Cat}">
<ObjectDataProvider.ConstructorParameters>
<System:String>Garfield</System:String>
</ObjectDataProvider.ConstructorParameters>
</ObjectDataProvider>
<ObjectDataProvider x:Key="Cat3" ObjectType="{x:Type local:Cat}">
<ObjectDataProvider.ConstructorParameters>
<System:String>Furball</System:String>
</ObjectDataProvider.ConstructorParameters>
</ObjectDataProvider>
In my UserControl I want to add the Cats to the Rooms:
<local:Room x:Name="Room1">
<local:Room.Cats>
</local:Room.Cats>
<local:Room>
<local:Room x:Name="Room2">
<local:Room.Cats>
</local:Room.Cats>
<local:Room>
What is the syntax for adding the Cat instances to the ObservableCollection Room.Cats? For instance I want to add Cat1 and Cat2 to Room1, and Cat2 and Cat3 to Room2. Am I completely on the wrong track?
Reusing individual instances the way you're trying to do it is tricky. This is because the way that you generally reference single objects in XAML is with the StaticResource
markup extension, and you can only use that markup extension to set a property value.
So you can easily set a property of type Cat
to an instance of a Cat
:
<Room Cat="{StaticResource Cat1}"/>
but you can't populate a collection by setting a property.
The answer, surprisingly, is to instantiate your objects directly in XAML instead of wrapping them in ObjectDataProvider
s. You still use the ObjectDataProvider
, but differently:
<Window x:Class="ObjectDataProviderDemo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:ObjectDataProviderDemo"
xmlns:Collections="clr-namespace:System.Collections;assembly=mscorlib"
Title="MainWindow"
Height="350"
Width="525">
<Window.Resources>
<local:Cat x:Key="Tom" Name="Tom"/>
<local:Cat x:Key="Garfield" Name="Garfield"/>
<local:Cat x:Key="Furball" Name="Furball"/>
<Collections:ArrayList x:Key="CatList1">
<ObjectDataProvider ObjectInstance="{StaticResource Tom}" />
<ObjectDataProvider ObjectInstance="{StaticResource Garfield}" />
<ObjectDataProvider ObjectInstance="{StaticResource Furball}" />
</Collections:ArrayList>
<Collections:ArrayList x:Key="CatList2">
<ObjectDataProvider ObjectInstance="{StaticResource Tom}" />
<ObjectDataProvider ObjectInstance="{StaticResource Furball}" />
</Collections:ArrayList>
<DataTemplate x:Key="CatTemplate">
<TextBlock Text="{Binding Name}" />
</DataTemplate>
</Window.Resources>
<StackPanel>
<ListBox ItemsSource="{StaticResource CatList1}"
ItemTemplate="{StaticResource CatTemplate}"/>
<ListBox ItemsSource="{StaticResource CatList2}"
ItemTemplate="{StaticResource CatTemplate}" />
</StackPanel>
</Window>
Based on the feedback from Heinzi and Robert Rossney I came up with the following solution that works with an ObservableCollection that I can access in XAML and code behind:
In code I extended ObservableCollection so I can use it in XAML (this will no longer be necessary in XAML 2009):
public class CatObservableCollection : ObservableCollection<Cat> { }
In XAML in the UserControl.Resources I instantiate the Cats:
<local:Cat x:Key="Tom" Name="Tom"/>
<local:Cat x:Key="Garfield" Name="Garfield"/>
<local:Cat x:Key="Furball" Name="Furball"/>
The collections:
<local:CatObservableCollection x:Key="Room1Collection">
<StaticResourceExtension ResourceKey="Tom"/>
<StaticResourceExtension ResourceKey="Garfield"/>
</local:CatObservableCollection>
<local:CatObservableCollection x:Key="Room2Collection">
<StaticResourceExtension ResourceKey="Garfield"/>
<StaticResourceExtension ResourceKey="Furball"/>
</local:CatObservableCollection>
The Rooms are now defined as follows:
<local:Room x:Name="Room1" Cats="{StaticResource Room1Collection}"/>
<local:Room x:Name="Room2" Cats="{StaticResource Room2Collection}"/>
Room.Cats is an ObservableCollection<Cat>
For your very specific needs, you to do go for generics. And to use generics declaratively, you have to use x:TypeArguments Directive. TypeArguments directive can be used only with root element. So, you have to now go for loose XAML file. A loose XAML file can be read using System.Windows.Markup.XamlReader.Load(Stream Stream) method
Cat.cs :
using System;
namespace WpfCollection._3840371
{
public class Cat
{
public Cat() { }
public Cat(String name, String color) { Name = name; Color = color; }
public String Name { get; set; }
public String Color { get; set; }
}
}
Room.cs :
using System;
using System.Collections.ObjectModel;
namespace WpfCollection._3840371
{
public class Room<T> where T : System.Windows.Data.ObjectDataProvider
{
public Room()
{
Cats = new ObservableCollection<T>();
}
public ObservableCollection<T> Cats { get; set; }
}
}
Window class :
namespace WpfCollection._3840371
{
/// <summary>
/// Interaction logic for Win3840371.xaml
/// </summary>
public partial class Win3840371 : Window
{
public Win3840371()
{
InitializeComponent();
Room<ObjectDataProvider> kitchenRoom;
using (FileStream fs = new FileStream(@"3840371/roomcats.txt", FileMode.Open))
{
kitchenRoom = (Room<ObjectDataProvider>)XamlReader.Load(fs);
}
foreach (ObjectDataProvider o in kitchenRoom.Cats)
Debug.WriteLine(((Cat)o.Data).Name + " : " + ((Cat)o.Data).Color);
}
}
}
So, your .txt file containing XAML code would be :
<local:Room
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:System="clr-namespace:System;assembly=mscorlib"
xmlns:local="clr-namespace:WpfCollection._3840371;assembly=WpfCollection"
x:Key="UpperRoom" x:TypeArguments="ObjectDataProvider">
<local:Room.Cats>
<ObjectDataProvider x:Key="Cat1" ObjectType="{x:Type local:Cat}">
<ObjectDataProvider.ConstructorParameters>
<System:String>Tom</System:String>
<System:String>Red</System:String>
</ObjectDataProvider.ConstructorParameters>
</ObjectDataProvider>
<ObjectDataProvider x:Key="Cat2" ObjectType="{x:Type local:Cat}">
<ObjectDataProvider.ConstructorParameters>
<System:String>Rubia</System:String>
<System:String>Brown</System:String>
</ObjectDataProvider.ConstructorParameters>
</ObjectDataProvider>
</local:Room.Cats>
</local:Room>