2011-11-06 87 views
0

我需要製作繪製內容的任意形狀的多邊形,例如圖片。 而多邊形是這樣的,一個部分是捆綁的,另一個部分必須依賴其父容器的大小。WPF內容的難區域

有必要製作一個XAML WPF。到目前爲止,沒有什麼我能想到的。 你有什麼想法嗎?

回答

1

你如何定義任意。有一個形狀空間。 XAML和後面代碼中的示例。 http://msdn.microsoft.com/en-us/library/ms747393.aspx。您需要在您後面的代碼中參考實際寬度和實際高度以獲取容器的渲染大小。您可以先畫一條對角線來確定尺寸,然後逐步增加一個多邊形。您可能希望在容器中使用畫布。

0

ОК。我得到了以下

C#代碼:

private void Grid_SizeChanged(object sender, SizeChangedEventArgs e) 
    { 
     BackgroundPathChange(); 
    }  

private void BackgroundPathChange() 
     {  
      int heightDesign = 300; 
      int widthDesign = 300; 

      var separatorLine = new List<Point>(){ 
            new Point(20, 1.1666667), 
            new Point(45.5, 110.99967), 
            new Point(45.5, 187.49966), 
            new Point(45.5, 237.49966), 
            new Point(19.5, 296.49966), 
            new Point(295.5, 295.49966), 
            new Point(289.5, 0.49966663) 
           }; 

      rightBackgroundPath.Data = Geometry.Parse(
       string.Format("M{0},{1} L{2},{3} {4},{5} {6},{7} {8},{9} {10},{11} {12},{13}", 
           (int)separatorLine[0].X, (int)(separatorLine[0].Y * gridContainer.ActualHeight/heightDesign), 
           (int)separatorLine[1].X, (int)(separatorLine[1].Y * gridContainer.ActualHeight/heightDesign), 
           (int)separatorLine[2].X, (int)(separatorLine[2].Y * gridContainer.ActualHeight/heightDesign), 
           (int)separatorLine[3].X, (int)(separatorLine[3].Y * gridContainer.ActualHeight/heightDesign), 
           (int)separatorLine[4].X, (int)(separatorLine[4].Y * gridContainer.ActualHeight/heightDesign), 

           (int)(separatorLine[5].X * gridContainer.ActualWidth/widthDesign), 
           (int)(separatorLine[5].Y * gridContainer.ActualHeight/heightDesign), 
           (int)(separatorLine[6].X * gridContainer.ActualWidth/widthDesign), 
           (int)(separatorLine[6].Y * gridContainer.ActualHeight/heightDesign) 
        )); 
     } 

XAML:

<Grid SizeChanged="Grid_SizeChanged" x:Name="gridContainer"> 
    <Path 
      x:Name="rightBackgroundPath" 
     Data="M20,1 L44,110.99967 45.5,187.49966 45.5,237.49966 19.5,300 300,300 300,0" 
      Fill="Yellow" Stroke="Green"/> 
</Grid> 

可以以某種方式簡化這個?

+0

什麼部分太複雜? 40行C#和4行XAML並不是很多代碼。它不工作?看起來你會因爲int轉換而失去一些精度。 – Paparazzi

+0

謝謝。鍵入Int - 僅測試,如果需要更改。我不喜歡C#的大代碼。 – Ivan