tags:

views:

180

answers:

2

Hello, i have written some code, which fills controls with some data from a database. Everything works fine if controls are put directly in a window. But how to fill controls, which are inside other controls, such as TabControls, GroupBoxes etc. My code looks like this:

Some window:

private void LoadDataP()
{
    if (ID.Length > 0)
    {
        if (baseButtons.LoadProcedureSelectName != string.Empty)
            LoadData = SqlHelper.GetTable(baseButtons.LoadProcedureSelectName, new string[] { IdName, ID });
        if (LoadData != null)
            foreach (DataRow dr in LoadData.Rows)
            {
                SqlHelper.FillWindowControllsWithData(myGrid, dr);
            }
    }

}

There are methods in another class.They do the main job:

public static void FillWindowControllsWithData(Grid windowGrid, DataRow dataRow)
{
    foreach (Control ctrl in windowGrid.Children)
    {
        FillWindowControllsWithData(ctrl, dataRow);
    }
}

public static void FillWindowControllsWithData(Control ctrl, DataRow dataRow)
{
    if (ctrl.Name.IndexOf("db_") == 0)
    {
        if (ctrl is TextBox)
        {
            if (dataRow.Table.Columns.Contains(ctrl.Name.Substring(3)))
            {
                ((TextBox)ctrl).Text = dataRow[ctrl.Name.Substring(3)].ToString();
            }
        }
    } //end if

}

So does anybody know how to fill data in a texbox, which is in some groupbox or tabcontrol, which also have children..?

+1  A: 

I think you could write the FillWindowControllsWithData method recursively. You need to alter the signature of the method though:

public void FillWindowControllsWithData(FrameworkElement element, DataRow dataRow)
{
    if (element is TextBox) 
   {
        if (element.Name.StartsWith("db_")
        {
            if (dataRow.Table.Columns.Contains(element.Name.Substring(3)))
            {
                ((TextBox)element).Text = dataRow[element.Name.Substring(3)].ToString();
            }
        }
    } 
     else if(element is Panel)
    {
       foreach(FrameworkElement el in ((Panel)element).Children)
       {
          FillWindowControllsWithData(el, dataRow);
       }
    }
    else if(element is ContentControl)
    {
      FillWindowControllsWithData(((ContentControl)element).Content, dataRow);
    }
    //else do nothing
}

However,this definitely is not the way to do it. You need to fill your controls through databinding. Start here to make a start. Good Luck!

Dabblernl
+1  A: 

Just to amplify on what Dabbleml says: Writing WPF applications using Windows Forms techniques is a road to despair and misery. The happy, joyous, and free way to build WPF applications is through the MVVM pattern and data-binding.

The buzzwords make this sound a lot harder than it actually is. In practice it's conceptually quite simple:

  1. Design your WPF window. This is your view.

  2. Build a class that has exposes each piece of data that your window will display as a property. This is your view model. Add a method to the class that populates it from the data source. (This, by the way, is your model. Now you can say that you're using the MVVM pattern and impress girls at parties.)

  3. Create a data context in your WPF window that will hold an instance of this class, and bind the controls to the class's properties.

What happens when you do this: the actual code you write all involves manipulating data, not the UI. When someone comes to you and asks if your window can display some kind of dancing bologna when some weird condition occurs, you don't write any UI code; you just add a property to your view model, and bind the dancing-bologna control in your view to it.

Robert Rossney