User Control Activation and Databinding

I

Iain

I've got a dialog with a tabbed control in. On some of the pages I've got
user controls whose (sub) controls are databound to an object (which
ultimately comes from a typed dataset).

All works fine EXCEPT that the data is not bound until the particular tab is
activated.

This is a problem if I want to change the data in just one of the panels in
the tab.

When I click OK, I run through the user controls and try and validate the
content. But as the data has not been bound from the underlying object, my
validation fails.

I note that the Load event (on the user control) is not called until the
first time the tab is activated.

So, how do I mimic clicking on the tab without changing focus ?

Thanks,

Iain
 
G

Guest

It depends on the binding context. For some reason tabControls can create there own bindingContext. The easist way I have found is to make sure that the tab control and pages have
the same binding context as the form

this.tabControl.BindingContext = this.BindingContext;
etc...

You need to do this before you start binding the controls on the tabControl and Tab Pages
 
I

Iain

Ah. Unfortunately, Many of the Tabs share Combo Boxes which I'm feeding
from a common source so I have to decouple them from each other by setting
separate binding contexts on each user control.

I tried it anyway and it made no difference.

But I don't hink that's it. Because, I would have expected the Load event
for each user control to be called when the form was initialised.

But it's not. It's not called until I click on the tab which the control is
contained in.

But the Binding occurs before load is called but seems not to work until the
user control is 'Activated' in some way.

Iain

Mike in Paradise said:
It depends on the binding context. For some reason tabControls can create
there own bindingContext. The easist way I have found is to make sure that
the tab control and pages have
the same binding context as the form

this.tabControl.BindingContext = this.BindingContext;
etc...

You need to do this before you start binding the controls on the
tabControl and Tab Pages
 
I

Iain

I'm still stuck on this one.

The problem is I have a parent and 5 child records, each held on a
separate tab of a tab control and each databound to various areas ofa
data set.

All Tab Pages except the main parent record have a single user control
which has a separate binding context (to bind combobox contents to
some tables without sharing the currently selected value).

The problem I have is that the values are not loaded into the fields
in the tab control until the Tab is physically selected.

As a result, when I try and save (having edited only the main record,
for example) my validation reports that all the other values are null.

The Load for the user controls does not occur until the Tab is
clicked.

Selecting the TabIndex does not cause the Load to ocurr or the binding
to occur.

I'm stumped.

Iain
 
I

Iain

I found the answer shortly after penning this (isn't data overload a
wonderful thing!).

Ying-Shen Yu of MSFT provided the solution in a different response.

It turns out that binding only occurs when a control is created and WinForms
does not create the control until it REALLY REALLY has to. Not even
selecting the tab or controls programatically does this unless you really
mean it.

So the solution is to create the controls manually. But this will only work
if the control's parent is visible.

So in the Activation event for the main form (once only) call this for each
control in the non visible tabs (remember my tabs each have one user control
in .. you could generalise this).

private void MakeItWork(Control AControl)
{
Control Parent = AControl.Parent;
AControl.Parent = this;
AControl.CreateControl();
foreach (Control child in AControl.Controls)
{
child.CreateControl();
}
AControl.Parent = Parent;
}

THis is a nasty solution and makes the initial display very slow. I may
rewrite to avoid databinding.

Iain
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top