2012-05-09 6 views
2

나는 DrawingVisual 개체를 가지고 있으며 채우기 및 획을 변경하고 싶습니다.DrawingVisual 채우기 및 획 바꾸는 방법

나는 채우기 위해 이것을 시도했다 : SetFill는

DrawingVisual MyDrawing = new DrawingVisual(); 
SetFill(Brushes.Red, MyDrawing.Drawing); 

입니다

여기서
private void SetFill(Brush fill, DrawingGroup group) 
{ 
    foreach (Drawing drw in group.Children) 
    { 
     if (drw is DrawingGroup) 
      SetFill(fill, drw as DrawingGroup); 
     else if (drw is GeometryDrawing) 
     { 
      GeometryDrawing geo = drw as GeometryDrawing; 
      geo.Brush = fill; 

      using (DrawingContext context = MyDrawing.RenderOpen()) 
      { 
       context.DrawDrawing(group); 
      } 
     } 
    } 
} 

그러나이 방법으로

내 DrawingVisual과는 다른 위치로 그려지는 경우가 발생할 수의 변환 마치 더 이상 (MyDrawing에) 적용되지 않았습니다. 이 서로 context.DrawDrawing(group); : :이 명령 변경하는 경우

또한, context.DrawDrawing(MyDrawing.Drawing); 을 나는 이상한 효과를 얻을 : 내가 처음에 아무 변화가없는 기입 변경하는 경우 채우기가의 위치를 ​​변경하지 않고 올바르게 변경되는 초 동안 그 그림.

어떻게하면됩니까?

+0

** 다시 그리기없이 ** GeometryDrawing.Brush'를 재귀 적으로 설정하려고 했습니까? – Clemens

+0

예, 불행히도 효과가 없습니다. – gliderkite

+0

"SetFillEx"를 호출 할 때마다 기존 DrawingGroup을 새 DrawingGroup의 Children 컬렉션에 넣으므로 "추한 솔루션"이 점점 더 많은 드로잉에 포함될 것입니다. – Clemens

답변

1

모든 채우기에 대해 SolidColorBrush을 사용하고 필요할 때마다 Color을 변경하는 것이 훨씬 간단합니다 (채우기를 동적으로 변경).

+0

각 DrawingVisual에 대한 SolidColorBrush를 의미합니까? 이런 식으로 내가 어떻게 바꿀 수 있니? 작은 예제를 만들 수 있습니까? Ps : 내 편집 본 적있어? – gliderkite

+0

'SetFill'에서 모든 GeometryDrawings에 대해 하나의 채우기 브러쉬를 설정하려고합니다. 따라서 모두 채우기 색상이 같습니다. 처음으로 그릴 때 모든 도면에 정확히 하나의 SolidColorBrush를 사용할 수 있습니다. 나중에 SolidColorBrush 만 변경하면 모든 도면의 채우기 색상이 변경됩니다. – Clemens

+0

수십만 개의 DrawingVisual이 있고 작업 부하를 늘리지 않기 때문에이 솔루션을 사용할 수 없습니다. 마지막 편집을 참조하십시오. – gliderkite

1

아마도이 문제를 이미 해결했지만 이미 여기에 나와있는 해결책이 같습니다. 나는 내 자신의 DrawingVisual 있습니다. 그것은 신선한 아니라 매우 코드를 테스트하지만 지금은 아주 좋은 일이다

public class MyDrawingVisual : DrawingVisual 
{ 
    private bool myIsSelected = false; 

    public VillageInfo VillageInfo { get; set; } 

    public bool IsSelected 
    { 
     get { return myIsSelected; } 
     set 
     { 
      if (myIsSelected != value) 
      { 
       myIsSelected = value; 

       // Retrieve the DrawingContext in order to create new drawing content. 
       DrawingContext drawingContext = this.RenderOpen(); 

       // Create a rectangle and draw it in the DrawingContext. 
       Rect rect = new Rect(new System.Windows.Point(VillageInfo.X + 0.1, VillageInfo.Y + 0.1), new System.Windows.Size(0.9, 0.9)); 
       drawingContext.DrawRectangle(new SolidColorBrush(myIsSelected ? Colors.White : VillageInfo.Color), (System.Windows.Media.Pen)null, rect); 

       // Persist the drawing content. 
       drawingContext.Close(); 
      } 
     } 
    } 
} 
1

당신은 참으로 비주얼을 다시 그릴 필요가 없습니다. 정상적으로 작동합니다.

private void SetFill(Brush fill, DrawingGroup group) 
{ 
    foreach (Drawing drw in group.Children) 
    { 
     if (drw is DrawingGroup) 
      SetFill(fill, drw as DrawingGroup); 
     else if (drw is GeometryDrawing) 
     { 
      GeometryDrawing geo = drw as GeometryDrawing; 
      geo.Brush = fill; // For changing FILL 

      // We have to set Pen properties individually. 
      // It does not work if we assign the whole "Pen" instance. 
      geo.Pen.Brush = fill; 
      geo.Pen.Thickness = 1.0; 
     } 
    } 
}