C# 单击按钮打开WPF弹出窗口

C# 单击按钮打开WPF弹出窗口,c#,wpf,visual-studio-2008,.net-3.5,popupwindow,C#,Wpf,Visual Studio 2008,.net 3.5,Popupwindow,我有一个WPF按钮,当单击按钮时,我试图在其上方或其顶部显示WPF弹出窗口: <StackPanel Grid.Column="3"> <Button x:Name="btnPrint" FocusVisualStyle="{x:Null}" Style="{DynamicResource MyButtonStyle}" Margin="15 5 5 5" Height="29" IsEnabled=

我有一个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>

但它不起作用。什么也没有发生。

创建一个新的wpf项目

还添加一个nuget引用以利用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>
视觉表现:


如果isPrintPopupOpen==值您是否缺少“=”或只是输入错误?如何更改isPrintPopupOpen的值?不会调用不正确的方法,也不会显示弹出窗口?@tabby是的,您是对的。这就是问题所在。@Babbillumpa如代码所示,我做了一个赋值,IsPrintPopupOpen=true。非常好的例子!
<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>