Saturday, September 11, 2010

Load Controls from WPF window using C++/CLI

void FindChildren(System::Windows::DependencyObject^ source, List^ pList)
{
System::Collections::Generic::List^ pDependencyObject = nullptr;
pDependencyObject = gcnew System::Collections::Generic::List();

if (source != nullptr)
{
GetChildObjects(source, pDependencyObject);

for each ( System::Windows::DependencyObject^ child in pDependencyObject)
{
if ( dynamic_cast(child) )
{
//MessageBox::Show ( "Yes Add" + child->GetType()->ToString());
pList->Add( (System::Windows::Controls::Control^)child );
}
//else
//MessageBox::Show ( "Not need" + child->GetType()->ToString());

}
}

}

void GetChildObjects(System::Windows::DependencyObject^ parent, System::Collections::Generic::List^ pDependencyObject)
{
bool bContentElement = false;
bool bFrameworkElement = false;

if (parent == nullptr)
return;

if ( dynamic_cast(parent) )
bContentElement = true;
else
bContentElement = false;


if ( dynamic_cast(parent) )
bFrameworkElement = true;
else
bFrameworkElement = false;

if ( ( bContentElement == true ) || ( bFrameworkElement == true ) )
{
for each (System::Object^ obj in System::Windows::LogicalTreeHelper::GetChildren(parent))
{
System::Windows::DependencyObject^ depObj = nullptr;
if ( dynamic_cast(obj) )
depObj = (System::Windows::DependencyObject^)obj;

if (depObj != nullptr)
{
pDependencyObject->Add( (System::Windows::DependencyObject^)obj );
//MessageBox::Show( ((System::Windows::DependencyObject^)obj)->GetType()->ToString() );
GetChildObjects( (System::Windows::DependencyObject^)obj, pDependencyObject );
}
}
}
}


call the function

List^ pControlList = nullptr;
pControlList = gcnew List();

FindChildren(pDialog, pControlList); //Load controls from dialog

for each ( System::Windows::Controls::Control^ pControl in pControlList )
{
//your checking
}

No comments: