How can I can check by tag if a Canvas
child exists?
For example I want to know if in my Canvas
(canvas1) there exists a child that has tag "p".
How can I can check by tag if a canvas child exist?
So I suppose a bool value is what you want
bool result = mycanvas.Children.Cast<FrameworkElement>()
.Any(x => x.Tag != null && x.Tag.ToString() == "p");
I used (x.Tag == null ? "" : x.Tag.ToString()) == "p")
just to be sure that tag property is not null.
OR
If you want the Element then
FrameworkElement result = mycanvas.Children.Cast<FrameworkElement>()
.Where(x => x.Tag != null &&
x.Tag.ToString() == "p")
.First();
object mytag;
DrawingCanvas.Children.Select((elem) =>
{
FrameworkElement fe = elem as FrameworkElement;
if (fe != null)
{
return fe.Tag == mytag;
}
else
return false;
});
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.