ContentPresenter无法找到控件

时间:2015-04-16 10:28:10

标签: c# wpf

我有一个ItemsControl定义如下

<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;
 }      

问题是cp的值总是为null,尽管自从我进入foreach循环后,itemscontrol中有一些项目。请帮忙

1 个答案:

答案 0 :(得分:1)

我认为问题出在Xaml,但我无法弄清楚什么是肯定的。也许其他风格?以下代码已经过测试并且运行良好

MainWindow XAML:

<ItemsControl x:Name="PlannerItemControl"  Width="100" Height="100">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Grid x:Name="MainGrid">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="200"/>
                        <ColumnDefinition Width="Auto" />
                        <ColumnDefinition />
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="*" />
                        <RowDefinition Height="auto" />
                    </Grid.RowDefinitions>
                    <TextBox x:Name="test" ></TextBox>

                </Grid>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
    <Button Height="20" Width="100" Click="ButtonBase_OnClick"> Click me!</Button>
            </StackPanel>

MainWindow.cs:

   public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            DataContext = new MainViewModel();
            PlannerItemControl.ItemsSource = new List<string>() {"a", "b", "c"};

        }



        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;
        }    

        private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
        {
            foreach (var item in PlannerItemControl.Items)
            {
                ContentPresenter cp = PlannerItemControl.ItemContainerGenerator.ContainerFromItem(item) as ContentPresenter;
                TextBox tb = FindVisualChild<TextBox>(cp);
                if (tb != null)
                {
                    tb.Text = item.ToString();
                    // do something with the textbox
                }
            }
        }
    }