1

I'm trying to add some ellipces with random positions into my canvas, but i can see them on my canvas. Progmab is compilling quite saccesfull. Code:

for (int i = 0; i < FirefliesCount; ++i)
            {
                Firefly CurrentFirefly = new Firefly();
                CurrentFirefly.Speed = Randomer.Next(1, 3);
                CurrentFirefly.Body = new Ellipse();
                CurrentFirefly.Body.Margin = new Thickness(Randomer.Next(10, (int)MainCanvas.Width - 10),
                                                           Randomer.Next(10, (int)MainCanvas.Height - 10),
                                                           0, 0);
                CurrentFirefly.Body.Fill = Brushes.Black;
                CurrentFirefly.Body.Height = MainCanvas.Height / 4;
                CurrentFirefly.Body.Width = 1.5 * CurrentFirefly.Body.Height;
                MainCanvas.Children.Add(CurrentFirefly.Body);
            }

And Fireflie class:

class Firefly
{
    public Ellipse Body { get; set; }
    public int Speed { get; set; }
}

1 Answer 1

1

Probably you did not set the Width and Height properties of your MainCanvas; then they have the value NaN and therefore you will not see the ellipses. My suggestion is to use ActualWidth and ActualHeight instead and to delay the adding of the ellipses until the canvas is loaded. Here is an example:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        MainCanvas.Loaded += MainCanvas_Loaded;
    }

    void MainCanvas_Loaded(object sender, RoutedEventArgs e)
    {
        Init();
    }

    private void Init()
    {
        const int FirefliesCount = 100;
        Random Randomer = new Random();

        for (int i = 0; i < FirefliesCount; ++i)
        {
            Firefly CurrentFirefly = new Firefly();
            CurrentFirefly.Speed = Randomer.Next(1, 3);
            CurrentFirefly.Body = new Ellipse();
            CurrentFirefly.Body.Margin = new Thickness(Randomer.Next(10, (int)MainCanvas.ActualWidth - 10),
                                                       Randomer.Next(10, (int)MainCanvas.ActualHeight - 10),
                                                       0, 0);
            CurrentFirefly.Body.Fill = Brushes.Black;
            CurrentFirefly.Body.Height = MainCanvas.ActualHeight / 4;
            CurrentFirefly.Body.Width = 1.5 * CurrentFirefly.Body.Height;
            MainCanvas.Children.Add(CurrentFirefly.Body);
        }
    }
}

The corresponding xaml file looks like this:

<Window x:Class="WpfApplication7.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Canvas x:Name="MainCanvas"/>
</Window>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.