picoe / Eto

Cross platform GUI framework for desktop and mobile applications in .NET
Other
3.67k stars 333 forks source link

WPF: Setting "Expanded" event fails when no datastore assigned #34

Closed carlokok closed 12 years ago

carlokok commented 12 years ago

NRE in TreeGridViewHandler.AttachEvent.

carlokok commented 12 years ago

The second time you set a DataStore it doesn't trigger OnExpanded anymore:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Eto;
using Eto.Forms;
using Eto.Drawing;
using System.Threading;

namespace etotest
{
    class Program
    {
        [STAThread]
        static void Main(string[] args)
        {
            var generator = Generator.GetGenerator("Eto.Platform.Wpf.Generator, Eto.Platform.Wpf");

            var app = new TestApplication(generator);
            app.Run(args);
        }
    }

    public class TestApplication : Application
    {
        public TestApplication(Generator generator)
            : base(generator)
        {
            this.Name = "Test Application";
        }

        public override void OnInitialized(EventArgs e)
        {
            this.MainForm = new MainForm();
            HandleEvent(Application.TerminatingEvent);

            base.OnInitialized(e);

            // show the main form
            this.MainForm.Show();
        }

    }

    public class MainForm : Form
    {
        int[] arr = new int[] { 5, 5 };
        TreeGridView tv;

        void CreateItem(TreeGridItem aParent, string aBaseName, int aLevel)
        {
            var lName = "[" + aLevel + "]" + aBaseName;
            var lItem = new TreeGridItem(lName);
            aParent.Children.Add(lItem);
            if (aLevel < arr.Length)
                for (int i = 0; i < arr[aLevel]; i++)
                    CreateItem(lItem, aBaseName + i.ToString(), aLevel + 1);
        }

        void CreateTree()
        {
            TreeGridItem lRoot = new TreeGridItem();
            CreateItem(lRoot, "Name", 0);

            Application.Instance.Invoke(delegate
            {
                tv.DataStore = lRoot;
                tv.Expanding += Expanding;
            });
        }

        public MainForm()
        {
            Size = new Size(900, 800);
            var lp = new Panel();
            this.AddDockedControl(lp, new Padding(5));
            var lPanelLayout = new TableLayout(lp, 1, 2);
            lPanelLayout.Padding = new Padding(0);
            var lButton = new Button();
            lButton.Text = "Rebuild";
            lButton.Click += but_Click;

            lPanelLayout.Add(lButton, 0, 0);

            tv = new TreeGridView();
            tv.Columns.Add(new GridColumn { DataCell = new TextBoxCell(0) });
            lPanelLayout.Add(tv, 0, 1, true, true);
        }

        void but_Click(object sender, EventArgs e)
        {
            CreateTree();

        }

        void Expanding(object o, EventArgs e)
        {
            MessageBox.Show("Expanding");
        }
    }
}
cwensley commented 12 years ago

Fixed with commit 62b60fb00d53f86f97693408e14f158abb792826

Thanks again for the repro!