2016-07-19 24 views
-1

RectangleText으로 구성된 사용자 정의 Shape을 작성했습니다.포함 된 도형에 다른 색상으로 설정 한 사용자 정의 모양 wpf

protected override Geometry DefiningGeometry 
    { 
     get 
     { 
      var formattedText = new FormattedText(Text, CultureInfo.CurrentCulture, FlowDirection.LeftToRight, new Typeface("Verdana"), 14, Brushes.Transparent); 
      var chosenTextPoint = new Point 
      { 
       X = ((Location.X < BottomRight.X) ? Location.X : BottomRight.X) + 5, 
       Y = ((Location.Y < BottomRight.Y) ? Location.Y : BottomRight.Y) + 5 
      }; 

      Stroke = Brushes.ForestGreen; 
      StrokeThickness = (IsSelected) ? HighlightedValue : HighlightedValue/2; 

      Rectangle = new Rect(Location, BottomRight); 

      var rectangleGeometry = new RectangleGeometry(Rectangle); 
      var textGeometry = formattedText.BuildGeometry(chosenTextPoint); 

      var combinedGeometry = new CombinedGeometry 
      { 
       GeometryCombineMode = GeometryCombineMode.Xor, 
       Geometry1 = rectangleGeometry, 
       Geometry2 = textGeometry 
      }; 

      combinedGeometry.Geometry1.SetValue(FillProperty, Brushes.Blue); 
      combinedGeometry.Geometry1.InvalidateProperty(FillProperty); 
      Fill = (IsSelected) ? Brushes.Transparent : null; 

      return combinedGeometry; 
     } 
    } 

combinedGeometry 내가 PathGeometry을 사용 그 전에 내가 최근에 추가 한 뭔가 : 여기에 코드입니다. 두 경우 모두 RectangleText은 같은 색으로 표시되며 동일한 모양 효과가 적용됩니다.

나는 두 가지 방법으로 구분할 수 있습니까? 분리하면 두 요소가 모두 Shape 안에있는 개별 요소가되며 둘 중 하나 또는 둘 모두를 자유롭게 수정할 수 있습니다.

+1

기하학에 채우기 속성을 설정할 수 없습니다. Shape에는 항상 1 개의 Fill 및 1 개의 Stroke가 있습니다. 다른 채우기/획을 원한다면 두 가지 모양을 사용하십시오. – Clemens

답변

-1

Geometry에는 색상이 없습니다. 이름 그대로입니다. Shape은 채우기 및 획 브러시가있는 단일 형상을 그립니다.

  • 를 사용하여 여러 모양 :

    당신도. 이것은 모든 Shape 여러 브러시로 여러 형상을 포함 할 수 있습니다 렌더링, 입력을받을 수있는 본격적인 제어 등

  • 사용하십시오 Drawing이 때문에 비용이 많이 드는, 다음 ImageSourceDrawingImage (유형을 사용하여 렌더링 할 수) 또는 일부 다른 컨트롤 (예 : Rectangle)의 채우기/배경으로 DrawingBrush (유형은 Brush입니다). Blend (Visual Studio와 함께 제공되는 도구)는 컨트롤 그룹을 DrawingBrush (도구> 브러시 만들기)으로 변환 할 수 있습니다.
  • 처음부터 컨트롤을 만들고 FrameworkElement에서 상속 받고 OnRender을 재정 의하여 직접 렌더링하십시오.