问题场景:
笔者自定义了一个GridContainer控件,并给GridContainer定义了一个Items依赖属性,把要放到gridContainer里显示的内容传递过去。当GridContainer所在的UserControl通过Xaml创建时一切都正常,但是当UserControl在通过代码创建时,发现总是找不到style里的Grid元素。后来查找资料才知道,这个时候是因为强制使用ApplyTemplate()方法。
//用于传递内容的依赖属性 public static readonly DependencyProperty ItemsProperty = DependencyProperty.Register(nameof(Items), typeof(IEnumerable<GridElement>), typeof(GridContainer), new PropertyMetadata(default(IEnumerable<GridElement>), new PropertyChangedCallback(CallMeWhenItemsFilled)));
//grid container style <Style TargetType="{x:Type customControls:GridContainer}"> <Setter Property="Template"> <Setter.Value> <ControlTemplate> <Grid x:Name="Container" /> </ControlTemplate> </Setter.Value> </Setter> </Style>
private static void CallMeWhenItemsFilled(DependencyObject d, DependencyPropertyChangedEventArgs e) { var items = e.NewValue as IEnumerable<GridElement>; var gridElements = (items ?? Array.Empty<GridElement>()).ToList(); if (d is GridContainer gridContainer) { //代码会卡在这里,总是找不到"container"元素 //gridContainer.GetTemplateChild("Container") 总是返回空值 //但是在gridContainer.Template通过Visual Studio调试的时候可以看到ChildNames里有这个元素 gridContainer.ApplyTemplate(); //这句是关键,加上这个后就可以再次找到"Container"元素
if (gridContainer.GetTemplateChild("Container") is Grid grid) { grid.ColumnDefinitions.Add(new ColumnDefinition {Width = GridLength.Auto}); grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1,GridUnitType.Star) }); var counter = 0; foreach (var frameworkElement in gridElements.Select(ResolveGridElementControls)) { grid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto, }); frameworkElement.Item1.SetValue(Grid.RowProperty, counter); frameworkElement.Item1.SetValue(Grid.ColumnProperty, 0); frameworkElement.Item2.SetValue(Grid.RowProperty, counter); frameworkElement.Item2.SetValue(Grid.ColumnProperty, 1); grid.Children.Add(frameworkElement.Item1); grid.Children.Add(frameworkElement.Item2); counter++; } } }