2013-08-29 3 views
0

я хочу изменить все цвета материала в 3d вид порта, используя этот код:проблемы в GetEnumerator в МОФ 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 before: 'if (model3d.Content! = Null && model3d.Content is GeometryModel3D) (model3d.Content как GeometryModel3D) .Material = mat;' – dkozl

ответ

1

ModelVisual3D.Content - это одиночный объект System.Windows.Media.Media3D, поэтому почему жалоба на foreach? Вместо внутреннего контура просто отбрасывать Content, как GeometryModel3D и изменить его 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; 
}