Click or drag to resize

Win Controller DataGrid

Windows DataGrid programming based on SOWI Win Helper.

Programming windows controller into a windows application based on SOWI Win Project Template file MainWindow.xaml and file MainWindow.xaml.cs

WPF Objects

  • TabControl object for DataGrid content

    <TabControl x:Name="TabMain" SelectionChanged="TabMain_SelectionChanged">
        <!-- * pattern *
        <TabItem Header="Organisation" >
            <Frame Name="FrameOrganisation" NavigationUIVisibility="Hidden" />
        </TabItem>
        -->
    </TabControl>

WPF Object Logic

  1. Constructor

    C#
    /// <summary>
    /// Initializes a new instance of the MainWindow class.
    /// Constructor without parameter
    /// </summary>
    public MainWindow()
    {
        InitializeComponent();
        this.InitializeFrame();
        this.TabMain.SelectedIndex = Properties.Settings.Default.TabPageIndex;
        //this.SliderViewFont.Value = Properties.Settings.Default.SliderViewFont;
        //this.StatusBarVersion.Content = "Program Version: " + App.Version;
        //this.SetForm();
    }
  2. Tabs and Controllers handling

    C#
    #region --- tabs and controllers handling ---
    
    /// <summary>
    /// Tab page enumeration
    /// </summary>
    private enum Tabs
    {
        // TODO: implement: extension of the tabs
        none = -1,
        SOWIA
    }
    
    /// <summary>
    /// Get or set tab page
    /// </summary>
    private Tabs TabPageIndex
    {
        get
        {
            return (Tabs)this.TabMain.SelectedIndex;
        }
        set
        {
            this.TabMain.SelectedIndex = (int)value;
        }
    }
    
    /// <summary>
    /// Tab selection changed
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void TabMain_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        Properties.Settings.Default.TabPageIndex = TabMain.SelectedIndex;
    
        // TODO: implement: specific tab selection handling
    
        //* switch command with tabs enumeration *
        //* command: switch (TabPageIndex) *
    }
    
    /// <summary>
    /// List of WPF frames with tabs index
    /// </summary>
    private Dictionary<Tabs, Frame> Frames { get; set; }
    
    /// <summary>
    /// List of SOWI windows controllers with tabs index
    /// </summary>
    private Dictionary<Tabs, object> Controllers { get; set; }
    
    /// <summary>
    /// Initialization of WPF frames
    /// </summary>
    private void InitializeFrame()
    {
        //* Frame object list with index of enumeration Tabs *
        this.Frames = new Dictionary<Tabs, Frame>();
    
        Tabs lTab;
        TabItem lTabItem;
        Frame lFrame;
    
        //* creates tab (TabItem and Frame) according to enumeration Tabs *
        foreach (var item in Enum.GetNames(typeof(Tabs)))
        {
            if (item.ToString() != Tabs.none.ToString())
            {
                lTab = SOWIData.Helper.Enumeration.Parse<Tabs>(item);
                lTabItem = new TabItem()
                {
                    Header = lTab.ToString()
                };
                lFrame = new Frame()
                {
                    Name = "Frame" + lTab.ToString(),
                    NavigationUIVisibility = System.Windows.Navigation.NavigationUIVisibility.Hidden
                };
                this.Frames.Add(lTab, lFrame);
                lTabItem.Content = lFrame;
                this.TabMain.Items.Add(lTabItem);
            }
        }
    }
    
    /// <summary>
    /// Initialization of SOWI Win controllers
    /// </summary>
    private void InitializeController()
    {
        this.Controllers = new Dictionary<Tabs, object>();
        if (this.Database != null)
        {
            string lNamespace = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name.ToString();
            SqlConnectionStringBuilder lConnectionString = new SqlConnectionStringBuilder(this.Database.ConnectionString);
            string lRoot = lConnectionString.InitialCatalog;
            Tabs lTab;
            foreach (var item in Enum.GetNames(typeof(Tabs)))
            {
                lTab = SOWIData.Helper.Enumeration.Parse<Tabs>(item);
                if (lTab != Tabs.none)
                {
                    lConnectionString.InitialCatalog = lRoot + lTab.ToString() + ".xml";
    
                    //* creates controller object *
                    Type lControllerType = Type.GetType(lNamespace + ".Controllers." + lTab.ToString() + "Controller");
                    Object[] lArgs = { lConnectionString.ConnectionString, this.UserName };
                    Object lController = Activator.CreateInstance(lControllerType, lArgs);
    
                    //* controller object adds to controller list *
                    this.Controllers.Add(lTab, lController);
                }
            }
        }
    }
    
    /// <summary>
    /// Returns the specific controller
    /// </summary>
    /// <param name="pController">Controller from the tab listing.</param>
    /// <param name="pDisplayMessage">When it's a wrong then display message.</param>
    /// <returns>Gives the controller or null</returns>
    /// <seealso cref="ExistsControllers(bool)"/>
    /// <seealso cref="Controllers"/>
    private SOWIWin.Helper.Interface.IControllerDataGrid GetController(Tabs pController, bool pDisplayMessage = true)
    {
        if (this.ExistsControllers(pDisplayMessage))
        {
            SOWIWin.Helper.Interface.IControllerDataGrid lController;
            try
            {
                lController = (SOWIWin.Helper.Interface.IControllerDataGrid)this.Controllers[pController];
            }
            catch (System.Exception ex)
            {
                if (pDisplayMessage)
                {
                    SOWIWin.Helper.Message.Show("Exception: " + ex.Message.ToString());
                }
                return null;
            }
            return lController;
        }
        return null;
    }
    
    /// <summary>
    /// Checks controllers instance
    /// </summary>
    /// <param name="pDisplayMessage">Display message (when nothing  instance)</param>
    /// <returns>If return value true then exists controllers instance</returns>
    /// <seealso cref="Controllers"/>
    private bool ExistsControllers(bool pDisplayMessage = true)
    {
        if (this.Controllers == null)
        {
            if (pDisplayMessage)
            {
                SOWIWin.Helper.Message.Show("No data loaded!");
            }
            return false;
        }
        return true;
    }
    
    /// <summary>
    /// Frame object contents handling
    /// </summary>
    private void SetFrame()
    {
        try
        {
            if (this.Database != null)
            {
                this.InitializeController();
    
                Tabs lTab;
                Frame lFrame;
                SOWIWin.Helper.Interface.IControllerDataGrid lController;
                foreach (var item in Enum.GetNames(typeof(Tabs)))
                {
                    lTab = SOWIData.Helper.Enumeration.Parse<Tabs>(item);
                    if (lTab != Tabs.none)
                    {
                        lFrame = this.Frames[lTab];
                        while (lFrame.NavigationService.RemoveBackEntry() != null) ;    //* remove all frames *
                        lController = GetController(lTab, false);
                        lFrame.Content = lController.Load();
                    }
                }
            }
        }
        catch (Exception ex)
        {
            SIC.Protocol.Write(ref ex);
            throw;
        }
    }
    
    #endregion
See Also