单击按钮打开WPF弹出窗口

时间:2018-07-10 09:15:23

标签: 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="{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");
    }
}

但不起作用。什么都没发生。

1 个答案:

答案 0 :(得分:2)

创建一个新的wpf项目。

https://www.nuget.org/packages/RelayCommand添加一个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>

视觉表示:

enter image description here