If you want to initialise a specific property on an object, you should use the Object Initialiser
syntax by naming the property you wish to initialise:
TreeViewItem newNode = new TreeViewItem()
{
Header = new StackPanel { Orientation = Orientation.Horizontal}
};
In your case, the compiler is telling you that you can't initialise a StackPanel
using the Collection Initializer
syntax.
This:
new StackPanel
{
Orientation.Horizontal
}
Will produce the error you're seeing:
Error 1 Cannot initialize type 'System.Windows.Controls.StackPanel' with a collection initializer because it does not implement 'System.Collections.IEnumerable'
Because you're trying to initialise the StackPanel as though it was a collection of System.Windows.Control.Orientation
objects, e.g. List<Orientation>
.