How to implement Objects Group

BitSmithy 2,161 Reputation points
2024-01-05T11:50:40.2366667+00:00

Hello,

I want to implement Objects Group something similaiar to GeometryGroup but on my own.

I created such code in c#

namespace MyNamespace
...

public class MyObjectGroup
{
	public List<MyBaseClass> Children {get; set;}
}

public Class MyBaseClass
{
}

public Class MyClass1 : MyBaseClass
{
	string MyProperty1 {get; set;}
}

public Class MyClass2 : MyBaseClass
{
	int MyProperty2 {get; set;}
}
...

Next in c# I want to use such syntax

...   
xmlns:local="using:MyNamespace" 
...

<Image Name="AAAA">
	<Image.Resources>
		<local:MyObjectGroup x:Key="XKMOG">
			<local:MyClass1 MyProperty1="AAAA"/>
			<local:MyClass2 MyProperty2="4"/>
		</MyObjectGroup>
	<Image.Resources>
</Image>
	

In my definition i can put MyObjectGroup to resources and app could be compiled, but If I add objects to MyObjectGroup - XAML reports an error:
"Class MyObjectGroup does not support direct content"

How to properly define MyObjectGroup, that could work. It works similiar to GeometryGroup

                <GeometryGroup x:Key="GG">
                    <RectangleGeometry></RectangleGeometry>
                    <EllipseGeometry></EllipseGeometry>
                </GeometryGroup>

Universal Windows Platform (UWP)
0 comments No comments
{count} votes

Accepted answer
  1. gekka 10,901 Reputation points MVP
    2024-01-07T00:26:55.0866667+00:00
    [Windows.UI.Xaml.Markup.ContentProperty(Name = nameof(Children))]
    public class MyObjectGroup
    {
        public List<MyBaseClass> Children
        {
            get => _Children ?? (_Children = new List<MyBaseClass>());
            set => _Children = value;
        }
        private List<MyBaseClass> _Children;
    }
    
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.