Home > OS >  GetTemplateChild on a custom ContentControl not working
GetTemplateChild on a custom ContentControl not working

Time:03-04

I have a custom ContentControl as follows.

Modal.xaml:

<Style TargetType="local:Modal">
        <Setter Property="Background" Value="#65000000" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="local:Modal">

                    <Grid Background="{TemplateBinding Background}" 
                          Padding="{TemplateBinding Padding}"
                          Visibility="{Binding IsOpen, RelativeSource={RelativeSource Mode=TemplatedParent}, Converter={StaticResource BoolToVisibilityConverter}}">

                        <Border x:Name="ContentBorder"
                                 Padding="80">
                            <ContentControl HorizontalContentAlignment="Stretch" 
                                            VerticalContentAlignment="Stretch">
                                <Grid x:Name="ContentGrid"
                                      HorizontalAlignment="Stretch"
                                      VerticalAlignment="Center"
                                      CornerRadius="10">
                                    <ContentPresenter Content="{TemplateBinding Content}"/>
                                </Grid>
                            </ContentControl>
                        </Border>

                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

How can I access the ContentBorder or ContentGrid controls inside of the code behind. I am saying if (control.GetTemplateChild("ContentBorder") is Border border) and it can't find it. Please help.

Code Behind is as follows:

Modal.cs:

 public class Modal : ContentControl
    {
        public Modal()
        {
            DefaultStyleKey = typeof(Modal);
        }

        public static readonly DependencyProperty IsOpenProperty =
            DependencyProperty.Register(nameof(IsOpen), typeof(bool), typeof(Modal), new PropertyMetadata(false));

        public bool IsOpen
        {
            get => (bool)GetValue(IsOpenProperty);
            set => SetValue(IsOpenProperty, value);
        }

        public static readonly DependencyProperty DialogMaxWidthProperty =
            DependencyProperty.Register(nameof(DialogMaxWidth), typeof(double), typeof(Modal), new PropertyMetadata(0, OnMaxWidthSet));

        private static void OnMaxWidthSet(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var control = (Modal)d;
            //GetTemplateChild is unable to find ContentBorder.
            if (control.GetTemplateChild("ContentBorder") is Border border) 
            {
                border.MaxWidth = (double)e.NewValue;
            }
        }

        public double DialogMaxWidth
        {
            get => (double)GetValue(DialogMaxWidthProperty);
            set => SetValue(DialogMaxWidthProperty, value);
        }
    }

I am using the custom control from somwhere outside as follows.

<controls:Modal x:Name="ContentModal"
                            Canvas.ZIndex="100"
                            Grid.Row="0"
                            Grid.RowSpan="2"
                            IsOpen="{x:Bind ViewModel.IsModalOpen, Mode=OneWay}"
                            DialogMaxWidth="450"/>

CodePudding user response:

if DialogMaxWidth property change execute before OnApplyTemplate, GetTemplateChild will return null. so you must add a field and set it on OnApplyTemplate function. then check it on propertychaged

private Border brd;

public override void OnApplyTemplate()
{
   brd = this.GetTemplateChild("ContentBorder") as Border;
   if (brd!=null && DialogMaxWidth!=0)
      brd.MaxWidth = DialogMaxWidth;
}


private static void OnMaxWidthSet(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var control = (Modal)d;
            //GetTemplateChild is unable to find ContentBorder.
            if (control.brd != null && control.brd is Border) 
            {
                control.brd.MaxWidth = (double)e.NewValue;
            }
        }
  • Related