单击按钮打开 WPF 弹出窗口

Opening a WPF popup on button click

我有一个 WPF 按钮,我试图在单击按钮时在其上方或顶部显示一个 WPF 弹出窗口:

<StackPanel Grid.Column="3">
    <Button x:Name="btnPrint"
        FocusVisualStyle="{x:Null}" 
        Style="{DynamicResource MyButtonStyle}"  
        Margin="15 5 5 5" Height="29"
        IsEnabled="{Binding BtnPrintEnabled}" 
        Command="{Binding btnPrintCommand}" 
        IsTabStop="False">
        Print
    </Button>
    <Popup IsOpen="{Binding IsPrintPopupOpen}" StaysOpen="False"  >
        <Border Background="LightYellow">
            <TextBlock>Sending to print ...</TextBlock>
        </Border>
    </Popup>
</StackPanel>

弹出窗口绑定到视图模型中的 属性,因此当单击按钮时,将执行按钮命令。命令在视图模型中执行以下方法:

private void ImprimirRemesa()
{
   IsPrintPopupOpen = true;

   // Do some things

   IsPrintPopupOpen = false;
}

private bool _isPrintPopupOpen = false;
public bool IsPrintPopupOpen
{
    get
    {
        return _isPrintPopupOpen;
    }

    set
    {
        if (_isPrintPopupOpen = value) return;

        _isPrintPopupOpen = value;
        OnPropertyChanged("IsPrintPopupOpen");
    }
}

但它不起作用。没有任何反应。

创建一个新的 wpf 项目。

也添加一个 nuget 引用 https://www.nuget.org/packages/RelayCommand 以利用 RelayCommand。

MainWindow.xaml.cs

using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Documents.DocumentStructures;
using System.Windows.Input;
using ButtonPopup.View.ViewModel;

namespace ButtonPopup
{
    /// <summary>
    ///     Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            DataContext = new DataContextObject();
        }
    }

    public class DataContextObject : INotifyPropertyChanged
    {
        private bool _isPrintPopupOpen;

        public bool IsPrintPopupOpen
        {
            get { return _isPrintPopupOpen; }

            set
            {
                if (_isPrintPopupOpen == value)
                {
                    return;
                }

                _isPrintPopupOpen = value;
                OnPropertyChanged(nameof(IsPrintPopupOpen));
            }
        }

        public ICommand PrintCommand => new RelayCommand(InitiatePrint);

        private async void InitiatePrint(object obj)
        {
            IsPrintPopupOpen = true;

            await Task.Delay(3000);

            IsPrintPopupOpen = false;
        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

MainWindow.xaml:

<Grid>
    <Button x:Name="btnPrint"
            Margin="15 5 5 5" Height="29"
            Command="{Binding PrintCommand}" 
            IsTabStop="False">
        Print
    </Button>
    <Popup IsOpen="{Binding IsPrintPopupOpen}" StaysOpen="False" PlacementTarget="{Binding ElementName=btnPrint}" >
        <Border Background="LightYellow">
            <TextBlock>Sending to print ...</TextBlock>
        </Border>
    </Popup>
</Grid>

视觉表示: