Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Define "wrapper"... Does it inherit from Rectangle or some other UIElement? The signature of Canvas.Children.Add requires something that derives from UIElement because it is a UIElementCollection.</p> <p>You would have to derive from UIElement, or a container UIElement such as Panel so you can add child objects such as a Rectangle.</p> <pre><code> public class MyRectangle : Panel { public MyRectangle() { this.Children.Add(new System.Windows.Shapes.Rectangle()); } } </code></pre> <p>Edit:</p> <p>You need to add your visual elements into the visual tree. Simply making them members of your class won't do. The xaml parser needs to know which members to add to the visual tree.</p> <p>Use the panel class which is a good starting point. It marks the class with the ContentProperty Attribute:</p> <pre><code>[ContentProperty("Children", true)] // Summary: // Specifies which property of a class can be interpreted to be the content // property when the class is parsed by a XAML processor. </code></pre> <p>if you just derive from Panel you gain that functionality. Then anything you want rendered in the visual tree you can just add to the "Children" property of your class.</p> <p>Or you could implement your own UIElementCollection and flag your class with the attribute</p> <pre><code>[ContentProperty("Children", true)] public class MyRectangle : UIElement { public MyRectangle() { this.Children = new UIElementCollection(); this.Children.Add(new System.Windows.Shapes.Rectangle()); } public UIElementCollection Children {get; private set;} } </code></pre> <p>One more edit:</p> <p>Then you also need to construct an instance of your rectangle and add it to the visual tree of your app. So your previous code would do:</p> <pre><code>MyRectangle rec = new MyRectangle(); canvas.Children.Add(rec); </code></pre> <p>Edit: Just tried to compile this and noticed you can't derive from UIElement... Try FrameworkElement or Control.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload