0

I want to append a child control (button) programmatically inside a border left corner at run time based on event of a check box. my code on check box true value:

var btn = new System.Windows.Controls.Primitives.ToggleButton();
btn.Style = App.Current.Resources["ToggleButtonStyle"] as Style;
ctrlBorder.Child = btn;
ctrlBorder.Tag = btn;

Now the issue is if i am adding as child, all other controls inside the control as invisible and only btn is displayed. How can I append a btn inside a border control. Also, on unchecking the checkbox the btn should disappear.

Any help is greatly appreciated!

Clemens
  • 123,504
  • 12
  • 155
  • 268
SamM
  • 1
  • 1
  • 3
  • You should first add a layout Panel (e.g. a StackPanel or a Grid) to the Border, then add new elements to the Children collection of the Panel. – Clemens Oct 31 '16 at 16:53

1 Answers1

0

By assigning ctrlBorder.Child you overwrite its only child (one allowed for border control). Try to append your control to the actual child of the border (Grid? StackPanel?)

see https://stackoverflow.com/a/1871229/1346098 :

Grid tmpGrid = ctrlBorder.Child as Grid;
tmpGrid.SetRow(btn, 3);
tmpGrid.SetColumn(btn, 4);
tmpGrid.Children.Add(btn);
Community
  • 1
  • 1
Sylvain P.
  • 153
  • 6