Windows Vista Forums

C#: Canvas.Loaded not firing
  1. #1


    znelson Guest

    C#: Canvas.Loaded not firing

    I have a Canvas that acts as a background image, let's call this
    bgCanvas. I also have another Canvas that contains a TextBlock, let's
    call this fgCanvas.

    bgCanvas always exists in the scene. An event is raised and as a
    result, I create a fgCanvas and add it to bgCanvas's children
    collection. Before I add it though, I wire-up fgCanvas.Loaded because
    I need to check it's ActualHeight property and you can't do that
    before it's calculated. So the idea is, create this canvas, wire up
    it's Loaded event, then add it and wait for the event to fire, then
    retrieve ActualHeight.

    Problem is Loaded never fires. I can visually see fgCanvas on the
    screen so Load must have occurred right? But the handler is never
    invoked.

    I tried LayoutUpdated but that fires way too often and it's a plain
    EventHandler with sender always null. I wish there was a
    SizeCalculated event or something.. any ideas?




      My System SpecsSystem Spec

  2. #2


    znelson Guest

    Re: C#: Canvas.Loaded not firing

    I worked around this by simplifying and such. I have the bgCanvas and
    I subclassed TextBlock which will lay on top. In the subclass I
    override OnRenderSizeChanged() and raise a custom event. This gets me
    the ActualHeight as soon as it's calculated.

    I didn't plan on having to create a custom eventhandler delegate and
    event args but unfortunately OnRenderSizeChanged() uses
    SizeChangedInfo yet RenderSizeEventHandler uses RenderSizeEventArgs
    and the two are not interchangeable. Not only that but when I tried to
    instantiate a RenderSizeEventArg I got an error saying there is no
    constructor defined.. WHAT?

    So in the end I had to roll my own TextBlock subclass, eventargs, and
    eventhandler. But it works and that's all that matters right?

    I'd still like to know why that Canvas was being rendered on the
    screen but Loaded was never firing....


      My System SpecsSystem Spec

  3. #3


    WPCoder Guest

    Re: C#: Canvas.Loaded not firing

    I'm not 100% sure what you're doing (so I may not be answering your question), but in a quick test with a Canvas called bgCanvas already on the form:

    public partial class Window1
    {
    private Canvas fgCanvas;

    public Window1()
    {
    this.InitializeComponent();
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
    fgCanvas = new Canvas();
    fgCanvas.Loaded += new RoutedEventHandler(fgCanvas_Loaded);
    fgCanvas.Width = 100;
    fgCanvas.LayoutUpdated += new EventHandler(fgCanvas_LayoutUpdated);
    bgCanvas.Children.Add(fgCanvas);
    }

    void fgCanvas_LayoutUpdated(object sender, EventArgs e)
    {
    txtSize.Text = fgCanvas.RenderSize.ToString();
    }

    void fgCanvas_Loaded(object sender, RoutedEventArgs e)
    {
    Console.WriteLine("Loaded");
    }
    }

    Both the Loaded and LayoutUpdated events are raised as expected. The sender arg is null though in the LayoutUpdated event -- so I needed to store a reference to the fgCanvas object as a private member variable. The txtSize TextBox I added gets the value of "100,0" when I click a button on the window.

    What I'm mostly confused about is that it seems like you're trying to get a size for the Canvas after layout? The Canvas normally doesn't size -- it's content can extend beyond it's boundaries without clipping, so it's size is normally irrelevant.

    I don't see any RenderSizeEventHanlder in the 3.0 documentation anywhere .... ?

    http://www.wiredprairie.us/journal



    "znelson" <znelson@gmail.com> wrote in message news:1173838305.954092.84480@n76g2000hsh.googlegroups.com...
    >I worked around this by simplifying and such. I have the bgCanvas and
    > I subclassed TextBlock which will lay on top. In the subclass I
    > override OnRenderSizeChanged() and raise a custom event. This gets me
    > the ActualHeight as soon as it's calculated.
    >
    > I didn't plan on having to create a custom eventhandler delegate and
    > event args but unfortunately OnRenderSizeChanged() uses
    > SizeChangedInfo yet RenderSizeEventHandler uses RenderSizeEventArgs
    > and the two are not interchangeable. Not only that but when I tried to
    > instantiate a RenderSizeEventArg I got an error saying there is no
    > constructor defined.. WHAT?
    >
    > So in the end I had to roll my own TextBlock subclass, eventargs, and
    > eventhandler. But it works and that's all that matters right?
    >
    > I'd still like to know why that Canvas was being rendered on the
    > screen but Loaded was never firing....
    >


      My System SpecsSystem Spec

C#: Canvas.Loaded not firing problems?

Similar Threads
Thread Thread Starter Forum Replies Last Post
dynamic loaded ascx delegate event not firing in parent form CMELLO .NET General 1 23 Feb 2010
DataGridView firing way to many SelectionChangedEvents! Please Help Simon Harvey .NET General 5 11 Aug 2008
Linkbutton firing every other time issue mazdotnet .NET General 0 30 Mar 2008
Triggers Firing for Child Objects gregbacchus@nospam.nospam Avalon 0 14 May 2007
firing noise in wmp11 Neerav Kothari Vista General 2 03 Apr 2007