简体   繁体   English

ContentPresenter找不到控件

[英]ContentPresenter can't find control

I have an ItemsControl defined as below 我有一个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>

In my code behind, I want to loop through all controls in an item 在后面的代码中,我想遍历项目中的所有控件

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

The problem is the the value of cp is always null although there are items in the itemscontrol since I go inside the foreach loop. 问题是尽管自从我进入foreach循环以来,虽然itemscontrol中有项目,但是cp的值始终为null。 Please help 请帮忙

I think the problem is in Xaml, but I cant figure out what is for sure. 我认为问题出在Xaml中,但我无法确定是什么。 Maybe other styles ? 也许其他样式? The following code is tested and running well 以下代码已经过测试并运行良好

MainWindow 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 : 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
                }
            }
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM