2009-06-27 56 views
2

如何通过某种名称删除wpf元素?所以某事像:如何通过标签名称删除画布上的wpf元素?

// Bar is some kind of usercontrol 
Bar b = new Bar(); 
b.Tag = "someId"; 
theCanvas.Children.Add(b); 

// Later to be removed without having the reference 
theCanvas.Children.RemoveElementWithTag("someId") 

除了ofcourse,RemoveElementWithTag是不是现有的方法...

回答

2

可以只使用一些LINQ:

var child = (from c in theCanvas.Children 
      where "someId".Equals(c.Tag) 
      select c).First(); 
theCanvas.Children.Remove(child); 

这么说,我高度怀疑有一个更清洁,更好的表现方式来实现你想要达到的目标。

+0

+1,因为你可以。我是这样做的,现在我正在使用一本字典。但是真的没有办法通过id或者某个元素来获取元素吗? – Peter 2009-06-27 20:48:15

相关问题