自定义 ContentControl 上的 GetTemplateChild 不起作用

GetTemplateChild on a custom ContentControl not working

我有一个自定义的 ContentControl,如下所示。

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>

如何在后面的代码中访问 ContentBorder 或 ContentGrid 控件。我说的是 if (control.GetTemplateChild("ContentBorder") is Border border) 但它找不到。请帮忙。

后面代码如下:

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

我在外面的某个地方使用自定义控件,如下所示。

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

如果 DialogMaxWidth 属性 更改在 OnApplyTemplate 之前执行,GetTemplateChild 将 return 为空。所以你必须添加一个字段并将其设置在 OnApplyTemplate 函数上。然后在 属性chaged

上查看
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;
            }
        }