我有一個ItemsControl定義如下ContentPresenter找不到控制
<ItemsControl Name="PlannerItemControl">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid Name="MainGrid">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="200"/>
<ColumnDefinition Width="Auto" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="auto" />
</Grid.RowDefinitions>
<TextBox Name="test" ></TextBox>
...
...
...
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
在後面我的代碼,我想通過所有的控制迴路中的一個項目
PlannerItemControl.ItemsSource = Plannermod.TimetableModelList;
foreach (var item in PlannerItemControl.Items)
{
ContentPresenter cp = PlannerItemControl.ItemContainerGenerator.ContainerFromItem(item) as ContentPresenter;
TextBox tb = FindVisualChild<TextBox>(cp);
if (tb != null)
{
// do something with the textbox
}
}
public static T FindVisualChild<T>(DependencyObject depObj) where T : DependencyObject
{
if (depObj != null)
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
{
DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
if (child != null && child is T)
{
return (T)child;
}
T childItem = FindVisualChild<T>(child);
if (childItem != null) return childItem;
}
}
return null;
}
問題是在因爲我進入了foreach循環,所以cp的值始終爲null,儘管itemscontrol中有項目。請幫助
這些可能會有所幫助:http://stackoverflow.com/questions/16184172/itemcontainergenerator -containerfromitem-returns-null-while-virtualizingstackp,http://stackoverflow.com/questions/6713365/itemcontainergenerator-containerfromitem-returns-null。 – goobering
也許你鑄造不正確。而不是轉換爲ContentPresenter,轉換爲FrameworkElement :)或從ItemsControl開始搜索。不需要容器。改變你的FindVisualChild尋找和匹配DataContext與項目 –
@dev hedgehog問題出現在FindVisualChild之前。 cp爲空。我試圖改變演員到FrameworkElement,但沒有幫助。還嘗試將項目直接傳遞給FindVisualChild(使用cast作爲dependencyobject),但這不起作用 – user2837961