在没有自定义键的情况下引用 UserControl?

Referencing UserControl without Custom Key?

所以我正在开发 C#/WPF 应用程序,我对 language/environment 比较陌生。几周前,我问了这个问题:

我想创建一种伪模态弹出窗口,只需单击一个按钮即可 appear/disappear。根据给我的答案,我创建了以下两个 类:

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;

namespace RelayC
{
    public partial class PopupBase : UserControl
    {
        public PopupBase()
        {
            this.Opacity = 0.0;
            this.Visibility = Visibility.Hidden;
        }

        private void OpenPopup()
        {
            this.Opacity = 1.0;
            this.Visibility = Visibility.Visible;
        }

        private void ClosePopup()
        {
            this.Opacity = 0.0;
            this.Visibility = Visibility.Hidden;

        }

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

        public static readonly DependencyProperty IsOpenProperty =
            DependencyProperty.Register(nameof(IsOpen),
                                        typeof(bool),
                                        typeof(PopupBase),
                                        new PropertyMetadata(false,
                                        new PropertyChangedCallback((s, e) =>
                                        {
                                            if (s is PopupBase popupBase && e.NewValue is bool boolean)
                                            {
                                                if (boolean)
                                                {
                                                    popupBase.OpenPopup();
                                                }
                                                else
                                                {
                                                    popupBase.ClosePopup();
                                                }
                                            }
                                        })));

    }

    public class PopupAttach
    {
        public static PopupBase GetPopup(ButtonBase button)
            => (PopupBase)button.GetValue(PopupProperty);

        public static void SetPopup(ButtonBase button, PopupBase value)
            => button.SetValue(PopupProperty, value);

        public static readonly DependencyProperty PopupProperty =
            DependencyProperty.RegisterAttached("Popup",
                                                typeof(PopupBase),
                                                typeof(PopupAttach),
                                                new PropertyMetadata(null,
                                                new PropertyChangedCallback((s, e) =>
                                                {
                                                    if (s is ButtonBase button && e.NewValue is PopupBase newPopup)
                                                    {
                                                        if (Application.Current.MainWindow.Content is Grid grid)
                                                        {
                                                            if (e.OldValue is PopupBase oldPopup)
                                                            {
                                                                grid.Children.Remove(oldPopup);
                                                            }

                                                            grid.Children.Add(newPopup);

                                                            button.Click -= buttonClick;
                                                            button.Click += buttonClick;
                                                        }
                                                        else
                                                        {
                                                            throw new Exception($"{nameof(Application.Current.MainWindow)} must have a root layout panel of type {nameof(Grid)} in order to use attachable Flyout.");
                                                        }

                                                        void buttonClick(object sender, RoutedEventArgs routedEventArgs)
                                                        {
                                                            newPopup.IsOpen = true;
                                                        }
                                                    }
                                                })));

    }
}

然后我创建了继承自 PopupBase 的任何 UserControl,因此我的 Popup UI 看起来有点像这样:

<local:PopupBase x:Class="RelayC.AddServer"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:RelayC"
             mc:Ignorable="d"
             Width="500"
             Height="375">

UI CONTENT INSERTED HERE

</local:PopupBase>

问题是当我想把它附加到一个按钮上时,我必须做这样的事情:

   <Window.Resources>
        <local:AddServer Grid.Row="1" x:Key="ISERCpm"/>
    </Window.Resources>

在我的 MainWindow.xaml 中,然后使用

从按钮调用它
<Button local:PopupAttach.Popup="{StaticResource ISERCpm}" />

是否可以跳过 Window 资源声明并直接调用我的 PopupBase 用户控件?

PopupAttach.Popup 是附加的依赖项 属性。可以使用 属性 元素语法而不是属性语法:

<Button>
  <local:PopupAttach.Popup>
    <local:AddServer Grid.Row="1"/>
  </local:PopupAttach.Popup>
</Button>