How to access a Control placed inside ListBox ItemTemplate in WP7(转)
In this post I am going to talk about how to access a Control inside the ListBox ItemPanelTemplate/DataTemplate in Silverlight for WP7.
Question: How to access/modify a specific Control placed inside ListBox ItemTemplate/DataTemplate?
When you have a data bound control, lets say for example ListBox we usually add some custom DataTemplate. So sometimes you try to access and modify any element inside DataTemplate.
Answer: Actually you can this by using the VisualTreeHelper which provides utility methods that can used to traverse object relationships (along child object or parent object axes) in the Silverlight for WP7 visual tree.
To begin with lets create a sample Windows Phone 7 project , add a data bound to a collection of strings ListBox with the following ItemsTemplate and ItemsPanel:
<ListBox x:Name="list">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<ToggleButton x:Name="btnToggle"/>
<CheckBox x:Name="cbx"/>
<TextBlock Text="{Binding}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>