I have a DerivedCanvas class which inherits from Canvas. I use it to draw several custom drawing visuals, but can't figure out how to add a Button object as a child of a DrawingVisual object. Below is the code I am using within the DrawingVisual, but nothing shows up at runtime. Is it not possible to add a Button as a child of a DrawingVisual, or am I just doing it wrong?
Button button = new Button { Content = "Exit", Width = 70, Height = 23 };
this.Children.Add(button);
Try this (assuming that dv
is your DrawingVisual
object and size
is the size of dv
):
foreach (var child in dv.Children)
{
var element = child as UIElement;
if (element == null) continue;
element.Arrange(new Rect(size));
}
1.Add try-catch to handle exception. 2.I guess button has created and added.But coordinate is out of visual range.You should alter coordinate of the button and refresh.
are you sure that your code is executed? Place a breakpoint. Also "this" might nor refer to the object you want..
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.