Servus,
hab ein kleines Problemchen mit einem Canvas in ner WPF.
Hab schonr elativ viel ausprobiert jedoch bekomm ichs nicht hin in ein Canvas zu zeichnen ohne direkt in der Main class zu sein.
ich poste einfach mal ein Beispiel
Lg DerGecko
in der main.xaml steht:
Code:
Canvas Height="288" HorizontalAlignment="Left" Margin="0,23,0,0" Name="myCanvas" VerticalAlignment="Top" Width="501" /
In der main.cs
Code:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
test re = new test();
re.paint(); <- geht nicht!
paint(); <- geht!
}
public void paint()
{
PointCollection points = new PointCollection();
points.Add(new Point(10, 0));
points.Add(new Point(0, 10));
points.Add(new Point(20, 10));
LinearGradientBrush brush = new LinearGradientBrush();
brush.StartPoint = new Point(0.5, 0);
brush.EndPoint = new Point(0.5, 1);
brush.GradientStops.Add(new GradientStop(Color.FromRgb(255, 0, 0), 0.0));
brush.GradientStops.Add(new GradientStop(Color.FromRgb(0, 255, 0), 0.0));
Polygon poly = new Polygon();
poly.Fill = brush;
poly.Points = points;
// startpos();
// Canvas.SetBottom(poly, pos_x);
myCanvas.Children.Add(poly);
}
}
class test : MainWindow
{
public test()
{ }
public void paint()
{
PointCollection points = new PointCollection();
points.Add(new Point(10, 0));
points.Add(new Point(0, 10));
points.Add(new Point(20, 10));
LinearGradientBrush brush = new LinearGradientBrush();
brush.StartPoint = new Point(0.5, 0);
brush.EndPoint = new Point(0.5, 1);
brush.GradientStops.Add(new GradientStop(Color.FromRgb(255, 0, 0), 0.0));
brush.GradientStops.Add(new GradientStop(Color.FromRgb(0, 255, 0), 0.0));
Polygon poly = new Polygon();
poly.Fill = brush;
poly.Points = points;
myCanvas.Children.Add(poly);
}
}