2013-01-07 129 views
0

我想知道如何在添加子項之前刷新畫布的方法? 我有這段代碼,它繪製了一個淺藍色的中風的黑色橢圓。現在我想在程序啓動時更改StrokeThickness(我有一個滑塊來定義StrokeThickness)。問題是StrokeThickness被改變了,但是隻有當我重繪橢圓時,但我希望在我移動滑塊時做出改變。有任何想法嗎?謝謝!Wpf畫布刷新

  //this code is in canvas_MouseDown 
      double smt = sliderThickness.Value; 

      //krog 
      elip = new Ellipse 
      { 
       Width = 100, 
       Height = 100, 
       Fill = Brushes.Black, 
       Stroke = Brushes.LightBlue, 
       StrokeThickness = smt, 
      }; 

      Canvas.SetLeft(elip, mouseX - 50); 
      Canvas.SetTop(elip, mouseY - 50); 

      canvas1.Children.Add(elip); 
+0

這應該沒有 「刷新」 工作。只需添加一次橢圓*,然後僅更改現有橢圓的StrokeThickness。 – Clemens

+0

但是我如何訪問現有的Ellipse? –

回答

0

或更短

// using System.Linq; 

foreach (var ellipse in canvas1.Children.OfType<Ellipse>()) 
{ 
    ellipse.StrokeThickness = sliderThickness.Value; 
} 
3
在滑塊值Changedevent

u必須把下面的代碼

private void sliderThickness_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e) 
    { 
     foreach (var item in canvas1.Children) 
     { 
      if (item is Ellipse) 
      { 
       var elip = item as Ellipse; 
       elip.StrokeThickness = sliderThickness.Value; 
      } 
     } 
    } 
+0

Sanjay Patel非常感謝,它的工作原理! :) –