如何从 Canvas 中删除标记的 WPF 对象?
How to remove a tagged WPF object from Canvas?
下面的代码给出了以下错误:
CS1061 UIElementCollection does not contain a definition for Where and
no accessible extension method Where accepting a first argument of
type UIElementCollection could be found (are you missing a using
directive or an assembly reference?)
var object = main.Children.Where(c => "platform1".Equals(c.Tag)).First();
main.Children.Remove(object);
如何让它工作?
Children
属性 类型 UIElementCollection
没有实现通用接口 IEnumerable<T>
,因此您不能将其用作 Enumerable
的源Where
.
等扩展方法
您必须添加类型转换方法,例如
var obj = main.Children.Cast<UIElement>().Where(...);
由于您还想访问 FrameworkElement
子类的 Tag
属性,请改用如下内容:
var obj = main.Children
.OfType<FrameworkElement>()
.Where(c => "platform1".Equals(c.Tag))
.First();
或更短:
var obj = main.Children
.OfType<FrameworkElement>()
.First(c => "platform1".Equals(c.Tag));
下面的代码给出了以下错误:
CS1061 UIElementCollection does not contain a definition for Where and no accessible extension method Where accepting a first argument of type UIElementCollection could be found (are you missing a using directive or an assembly reference?)
var object = main.Children.Where(c => "platform1".Equals(c.Tag)).First();
main.Children.Remove(object);
如何让它工作?
Children
属性 类型 UIElementCollection
没有实现通用接口 IEnumerable<T>
,因此您不能将其用作 Enumerable
的源Where
.
您必须添加类型转换方法,例如
var obj = main.Children.Cast<UIElement>().Where(...);
由于您还想访问 FrameworkElement
子类的 Tag
属性,请改用如下内容:
var obj = main.Children
.OfType<FrameworkElement>()
.Where(c => "platform1".Equals(c.Tag))
.First();
或更短:
var obj = main.Children
.OfType<FrameworkElement>()
.First(c => "platform1".Equals(c.Tag));