Suppose you have the following xaml view -
<Grid x:Name="LayoutRoot" Background="White">
<StackPanel x:Name="OuterStackPanel1">
<TextBox x:Name="TextBox1" Text="This is a text Box - 1"/>
<ListBox x:Name="MyListBox">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel x:Name="ListBoxStackPanel" Orientation="Horizontal">
<CheckBox x:Name="CharanChkBox" Content="Select Charan"/>
<CheckBox x:Name="DhananChkBox" Content="Select Dhananjay"/>
<TextBox x:Name="TTTBox" Width="300" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<Button Content="Find TextBox" x:Name="btnFind" Click="btnFind_Click" />
</StackPanel>
</Grid>
Now you are trying to find the control CharanChkBox,DhananChkBox and TTTBox in your View or ViewModel. Normally LayoutRoot(of type Grid) has a method as FindName which can be used to find the instance of the Control inside View or ViewModel as below -
var txtBox = LayoutRoot.FindName("MyListBox") as TextBox;
var listBox = LayoutRoot.FindName("MyListBox") as ListBox;
However, the following will return null value for templatedtxtBox-
var templatedtxtBox = LayoutRoot.FindName("TTTBox") as TextBox;
Reason being the above control is just a Templated column and it has not been created utill the MyListBox is created and Loaded. Once it's Load we can have the following call to get the container of the item for a specified index -
var container = (FrameworkElement)itemControl.ItemContainerGenerator.ContainerFromIndex(SelectedIndex);
Once the item container is found we can iterate the child/templated controls using
VisualTreeHelper.
The Full code sample is as below -
public UIElement DrillTemplatedItemControl(ItemsControl itemControl, int SelectedIndex, string childxName)
{ UIElement result = null;var container = (FrameworkElement) itemControl.ItemContainerGenerator.ContainerFromIndex(SelectedIndex);
if (container != null)
result = FindDescendant<UIElement>(container, childxName) as UIElement;
return result;
}
public T FindDescendant<T>(DependencyObject obj, string xName) where T : DependencyObject
{
// Check if this object is the specified type
if (obj is T){
FrameworkElement d = obj as FrameworkElement;
if (d.Name == xName) return obj as T;
}// Check for children
int childrenCount = VisualTreeHelper.GetChildrenCount(obj);
if (childrenCount < 1) return null;
// First check all the children
for (int i = 0; i < childrenCount; i++)
{
DependencyObject child = VisualTreeHelper.GetChild(obj, i);
if (child is T)
{FrameworkElement d = child as FrameworkElement;
if (d.Name == xName) return child as T;
}
}
// Then check the childrens children
for (int i = 0; i < childrenCount; i++)
{DependencyObject child = FindDescendant<T>(VisualTreeHelper.GetChild(obj, i), xName);
if (child != null && child is T)
{FrameworkElement d = child as FrameworkElement;
if (d.Name == xName) return child as T;
}
}
return null;
}
To Use the above method make the call as below (1st parameter is the ItemControl instance,index value,Control Name you are looking for) -
CheckBox result = DrillTemplatedItemControl(MyListBox, 0, "CharanChkBox") as CheckBox;
No comments:
Post a Comment