mercredi 28 avril 2010

Find a control in a WPF/Silverlight by name

Public static T FindVisualChildByName(DependencyObject parent, string name) where T : DependencyObject

for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
var child = VisualTreeHelper.GetChild(parent, i);
string controlName = child.GetValue(Control.NameProperty) as string;
if (controlName == name)
return child as T;
T result = FindVisualChildByName(child, name);
if (result != null)
return result;
return null;
This works for the Visual elements, not for the current elements of a parent:


mercredi 21 avril 2010

SL Custom Controls

Step 1: Create a New Silverlight Project
Step 2: Derive from Control (or ContentControl)
Step 3: Create a Control Template
Step 4: Create a Default Control Template
Step 5: Add Template Bindings
Step 6: Replace TextBlock with ContentPresenter
Step 7: Add a Click Event
Step 8: Add Visual States
The Completed Control


Silverlight: Refresh DataGrid