如何在我的自定义控制中更改儿童控制的可见性?

发布于 2025-01-18 04:09:07 字数 851 浏览 3 评论 0原文

我继承了 HeaderedContentControlControl。当用户单击父控件时,我需要隐藏子控件,但我不知道如何访问其子控件。

这是我的代码。

public class MyHeaderedContentControl : HeaderedContentControl
{
    public static readonly DependencyProperty ClickToHideProperty =
        DependencyProperty.Register("ClickToHide", typeof(bool), typeof(MyHeaderedContentControl),
            new PropertyMetadata(null));

    public bool ClickToHide
    {
        get => (bool)GetValue(ClickToHideProperty);
        set => SetValue(ClickToHideProperty, value);
    }

    protected override void OnPreviewMouseLeftButtonDown(MouseButtonEventArgs e)
    {
        base.OnPreviewMouseLeftButtonDown(e);

        //TODO Hide one child control when user clicked the parent control

        e.Handled = true;
    }
}

I inherited my Control for HeaderedContentControl. I need to hide the child control when user clicked the parent control, but I do not know how to access its child control.

Here is my code.

public class MyHeaderedContentControl : HeaderedContentControl
{
    public static readonly DependencyProperty ClickToHideProperty =
        DependencyProperty.Register("ClickToHide", typeof(bool), typeof(MyHeaderedContentControl),
            new PropertyMetadata(null));

    public bool ClickToHide
    {
        get => (bool)GetValue(ClickToHideProperty);
        set => SetValue(ClickToHideProperty, value);
    }

    protected override void OnPreviewMouseLeftButtonDown(MouseButtonEventArgs e)
    {
        base.OnPreviewMouseLeftButtonDown(e);

        //TODO Hide one child control when user clicked the parent control

        e.Handled = true;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情感失落者 2025-01-25 04:09:07

您可以设置 子控件的可见性,即内容

protected override void OnPreviewMouseLeftButtonDown(MouseButtonEventArgs e)
{
   base.OnPreviewMouseLeftButtonDown(e);

   // Only a click on this control is handled, not children.
   if (!e.Source.Equals(this))
      return;

   // The content must be at least a UI element in order to access the Visibility property.
   if (Content is UIElement uiElement)
   {
      // If the child (Content) is visible, collapse it, if it is collapsed, make it visible.
      uiElement.Visibility = uiElement.Visibility == Visibility.Collapsed
         ? Visibility.Visible
         : Visibility.Collapsed;
   }

   e.Handled = true;
}

如果您创建自定义控件,则也可以指定 TemplatePart 对于 ContentPresenter 。然后,您始终可以显示和隐藏显示 ContentContentPresenter,而不是依赖于实际的子控件。这也适用于非 UIElement 子元素,例如 string

[TemplatePart(Name = "PART_ContentPresenter", Type = typeof(ContentPresenter))]
public class MyHeaderedContentControl : HeaderedContentControl
{
   private ContentPresenter _contentPresenter;

   public static readonly DependencyProperty ClickToHideProperty =
      DependencyProperty.Register("ClickToHide", typeof(bool), typeof(MyHeaderedContentControl),
         new PropertyMetadata(null));

   public bool ClickToHide
   {
      get => (bool)GetValue(ClickToHideProperty);
      set => SetValue(ClickToHideProperty, value);
   }

   public override void OnApplyTemplate()
   {
      base.OnApplyTemplate();
      _contentPresenter = GetTemplateChild("PART_ContentPresenter") as ContentPresenter;
   }

   protected override void OnPreviewMouseLeftButtonDown(MouseButtonEventArgs e)
   {
      base.OnPreviewMouseLeftButtonDown(e);

      // Only a click on this control is handled, not children.
      if (_contentPresenter is null || !e.Source.Equals(this))
         return;

      // If the child (Content) is visible, collapse it, if it is collapsed, make it visible.
      _contentPresenter.Visibility = _contentPresenter.Visibility == Visibility.Collapsed
            ? Visibility.Visible
            : Visibility.Collapsed;

      e.Handled = true;
   }
}

唯一的要求是您的控件模板包含这部分。

<Style TargetType="{x:Type local:MyHeaderedContentControl}">
   <Setter Property="Template">
      <Setter.Value>
         <ControlTemplate TargetType="{x:Type local:MyHeaderedContentControl}">
            <ContentPresenter x:Name="PART_ContentPresenter"/>
         </ControlTemplate>
      </Setter.Value>
   </Setter>
</Style>

请参阅创建具有可自定义外观的控件以供参考。

You could set the Visibility of the child control, which is the Content of the control.

protected override void OnPreviewMouseLeftButtonDown(MouseButtonEventArgs e)
{
   base.OnPreviewMouseLeftButtonDown(e);

   // Only a click on this control is handled, not children.
   if (!e.Source.Equals(this))
      return;

   // The content must be at least a UI element in order to access the Visibility property.
   if (Content is UIElement uiElement)
   {
      // If the child (Content) is visible, collapse it, if it is collapsed, make it visible.
      uiElement.Visibility = uiElement.Visibility == Visibility.Collapsed
         ? Visibility.Visible
         : Visibility.Collapsed;
   }

   e.Handled = true;
}

If you create a custom control, you could alternatively specify a TemplatePart for a ContentPresenter. Then you can always show and hide the ContentPresenter which shows the Content, instead of depending on the actual child controls. This would also work for non-UIElement children like a string.

[TemplatePart(Name = "PART_ContentPresenter", Type = typeof(ContentPresenter))]
public class MyHeaderedContentControl : HeaderedContentControl
{
   private ContentPresenter _contentPresenter;

   public static readonly DependencyProperty ClickToHideProperty =
      DependencyProperty.Register("ClickToHide", typeof(bool), typeof(MyHeaderedContentControl),
         new PropertyMetadata(null));

   public bool ClickToHide
   {
      get => (bool)GetValue(ClickToHideProperty);
      set => SetValue(ClickToHideProperty, value);
   }

   public override void OnApplyTemplate()
   {
      base.OnApplyTemplate();
      _contentPresenter = GetTemplateChild("PART_ContentPresenter") as ContentPresenter;
   }

   protected override void OnPreviewMouseLeftButtonDown(MouseButtonEventArgs e)
   {
      base.OnPreviewMouseLeftButtonDown(e);

      // Only a click on this control is handled, not children.
      if (_contentPresenter is null || !e.Source.Equals(this))
         return;

      // If the child (Content) is visible, collapse it, if it is collapsed, make it visible.
      _contentPresenter.Visibility = _contentPresenter.Visibility == Visibility.Collapsed
            ? Visibility.Visible
            : Visibility.Collapsed;

      e.Handled = true;
   }
}

The only requirement would be that your control template contains this part.

<Style TargetType="{x:Type local:MyHeaderedContentControl}">
   <Setter Property="Template">
      <Setter.Value>
         <ControlTemplate TargetType="{x:Type local:MyHeaderedContentControl}">
            <ContentPresenter x:Name="PART_ContentPresenter"/>
         </ControlTemplate>
      </Setter.Value>
   </Setter>
</Style>

See Creating a Control That Has a Customizable Appearance for reference.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文