Canvas Example


The most common use of the Canvas may be to display dialogs, see Dialogs.

You also may want to use it as a way to manage any fixed layout of items at X-Y coordinates . . . or to enable drag-and-drop scenarios on a simulated desktop?

However, besides these more obvious examples, you could also use it in the small, as merely a way to offset labels or other Framework Elements, since you may hang labels outside of its boundary, over its edge. See pic above.

Here is the code for it. I happen to be nesting one StackPanel child inside an existing StackPanel parent using the AddAnyIParent syntax. Then, inside of the lambda expression, I initialize the StackPanel with a Canvas as the Composite for any/all children created within that StackPanel:

.AddAnyIParent<StackPanel, StackPanel>(0, 7, myStkPanel =>
            myStkPanel.Initialize(100, 200, ContainerType.Canvas, Orientation.Vertical);
            //Set Default Colors
            myStkPanel.SetCompositeCanvasSettings(100, 50, Brushes.Red, Brushes.Green);

            myStkPanel.Background = Brushes.LightSlateGray;
            myStkPanel.Opacity = .59;
             .SetTextColorForLabel(30, 35, Brushes.White)
             .SetZIndex<Label, StackPanel>(10, 25, 2)
             .SetFontOnLabel(10, 10, "Arial", 11, FontWeights.DemiBold, FontStyles.Normal)
             .SetFontOnLabel(30, 10, "Arial", 11, FontWeights.DemiBold, FontStyles.Italic)
             .SetFontOnLabel(10, 25, "Arial", 14, FontWeights.Bold, FontStyles.Normal)
             .SetFontOnLabel(30, 35, "Arial", 14, FontWeights.Bold, FontStyles.Italic)
            Border xty = myStkPanel.BeginComposite()
              .AddLabel(10, 10, "This is a label on a Canvas #1:", Brushes.White)
              .AddLabel(30, 10, "another label", Brushes.Plum)
            .EndComposite<StackPanel, StackPanelArgs>(null);
            //Override Default Colors
            myStkPanel.GetContainerFromComposite<Canvas, StackPanel>(myStkPanel.GetCompositeFromParent(xty.GetKey())).SetBackgroundColor(Brushes.Khaki);
            myStkPanel.GetContainerFromComposite<Canvas, StackPanel>(myStkPanel.GetCompositeFromParent(xty.GetKey())).SetMouseOverColor(Brushes.Green);
              .AddLabel(10, 25, "This is a label on a Canvas #2:", Brushes.White)
              .AddLabel(30, 35, "yet another label", Brushes.Blue)
            .EndComposite<StackPanel, StackPanelArgs>(null);

Note that I use the special method SetCompositeCanvasSettings to define a default width, height, background color, and mouseover color, for any canvas composites added to the Parent StackPanel. I also show an example with the first child canvas of getting a handle to it and overriding these default colors.

Last edited Mar 31, 2014 at 6:44 AM by stagathome0069, version 11


No comments yet.