Page 1 of 1

Initialized event never raised on TChart

Posted: Thu Oct 09, 2008 9:17 am
by 13050248
I've tried to attach to the initialized event in XAML. The event never got fired. I've also tried to attach to the mouse double click event and that also never got fired, but that isn't illustrated in this example.

The XAML:

Code: Select all

<Window x:Class="WPFDemo.Window3"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:Chart="clr-namespace:Steema.TeeChart.WPF;assembly=TeeChart.WPF"
    Title="Window3" Height="300" Width="300">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Label Grid.Row="0" Content="My Chart" Initialized="Label_Initialized"/>
        <Chart:TChart Grid.Row="1" Margin="0,0,0,0" Name="Chart" Initialized="Chart_Initialized"/>
    </Grid>
</Window>
The code behind:

Code: Select all

using System;
using System.Windows;

namespace WPFDemo
{
    /// <summary>
    /// Interaction logic for Window3.xaml
    /// </summary>
    public partial class Window3 : Window
    {
        public Window3()
        {
            InitializeComponent();
        }

        private void Label_Initialized(object sender, EventArgs e)
        {
            MessageBox.Show("Label Initialized.");
        }

        private void Chart_Initialized(object sender, EventArgs e)
        {
            MessageBox.Show("Chart Initialized.");
        }
    }
}

Re: Initialized event never raised on TChart

Posted: Thu Oct 09, 2008 2:08 pm
by Chris
Hello Christo,
Christo Zietsman wrote:I've tried to attach to the initialized event in XAML. The event never got fired. I've also tried to attach to the mouse double click event and that also never got fired, but that isn't illustrated in this example.
Strange. TChart derives from UserControl, so it should work. In fact, I created a very simple UseControl like this:

Code: Select all

namespace MyUserControl
{
  /// <summary>
  /// Interaction logic for UserControl1.xaml
  /// </summary>
  public partial class UserControl1 : UserControl
  {
    public UserControl1()
    {
      InitializeComponent();
    }

    protected override void OnRender(DrawingContext drawingContext)
    {
      Rect rect = new Rect(RenderSize);
      drawingContext.DrawRectangle(Brushes.AliceBlue, new Pen(Brushes.Red, 1), rect);
      base.OnRender(drawingContext);
    }
  }
}
and then used it in the XAML of a standard WPF window like this:

Code: Select all

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:mc="clr-namespace:MyUserControl;assembly=MyUserControl"
    Title="Window1" Height="351" Width="496">
    <Grid>
    <mc:UserControl1 Margin="0,0,0,0" Name="uControl1" Initialized="uControl1_Initialized"/>
  </Grid>
</Window>
with the code behind like this:

Code: Select all

    private void uControl1_Initialized(object sender, EventArgs e)
    {
      MessageBox.Show("Hello!");
    }
this doesn't work either. Maybe this is a WPF bug? Can you reproduce the above at your end?

Posted: Fri Oct 10, 2008 6:41 am
by 13050248
Hi Christopher,

It didn't work on my side either. Maybe this is a WPF bug as you suggested. UserControl & Label both derive from ContentControl. I spied around using .NET Reflector and there isn't much difference between these two controls, but I'm not an WPF framework expert.

Posted: Fri Oct 10, 2008 11:10 am
by narcis
Hi Christo,

We have done some research on the issue in some forums and so far we have been suggested that, as a workaround, you could try using the Loaded event instead, which is fired after the control is laid out and rendered.

Posted: Fri Oct 10, 2008 11:47 am
by 13050248
Hi NarcĂ­s,

Yes that is the solution I came up with too.

Posted: Fri Oct 10, 2008 1:19 pm
by Chris
Hello,
Christo Zietsman wrote:Yes that is the solution I came up with too.
For completeness, you might like to have a look at this MSDN Forum thread.

As there is no way for you to subscribe to the event before the InitializeComponent() method of TChart, then using the Loaded event seems the best option.