2013-08-29 54 views
0

我想改變這一切材料顏色使用此代碼視圖口3D:麻煩的GetEnumerator在WPF C#

DiffuseMaterial mat = new DiffuseMaterial(new SolidColorBrush(Colors.Red)); 

     foreach (ModelVisual3D model3d in previewport.Children) 
     { 

      foreach (GeometryModel3D item in model3d.Content) 
      { 
       item.Material = mat; 
      } 
     } 

但得到的錯誤:

 Error 
    foreach statement cannot operate on variables of type 'System.Windows.Media.Media3D.Model3D' because 'System.Windows.Media.Media3D.Model3D' does not contain a public definition for 'GetEnumerator' 

請幫助。 謝謝。

+0

你不應該使用model3d.Children代替model3d.Content? – Nitin

+0

@SmartMan,你的意思是:'(model3d.Content as GeometryModel3D).Material = mat;'而不是內部的'foreach'循環? – dkozl

+0

@SmartMan,如果之前添加:if(model3d.Content!= null && model3d.Content is GeometryModel3D)(model3d.Content as GeometryModel3D).Material = mat;' – dkozl

回答

1

ModelVisual3D.Content是一個單一的System.Windows.Media.Media3D對象,這就是爲什麼它抱怨foreach循環。相反,內環只投ContentGeometryModel3D並改變其Material象下面這樣:

DiffuseMaterial mat = new DiffuseMaterial(new SolidColorBrush(Colors.Red)); 

foreach (ModelVisual3D model3d in previewport.Children) 
{ 
    var geometryModel = model3d.Content as GeometryModel3D; 
    if (geometryModel != null) geometryModel.Material = mat; 
}