【问题标题】:Opening a WPF popup on button click单击按钮时打开 WPF 弹出窗口
【发布时间】:2018-12-18 01:47:50
【问题描述】:

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

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

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");
    }
}

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

【问题讨论】:

  • if(_isPrintPopupOpen == value) 你是缺少 '=' 还是只是一个错字
  • 如何更改 IsPrintPopupOpen 的值?
  • 没有调用ImprimirRemesa() 方法中的“没有任何反应”,或者没有显示弹出窗口中的“没有任何反应”?
  • @tabby 是的,你是对的。这就是问题所在。
  • @Babbillumpa 如代码所示,我做了一个赋值,IsPrintPopupOpen = true。

标签: c# wpf visual-studio-2008 .net-3.5 popupwindow


【解决方案1】:

创建一个新的 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>

视觉表现:

【讨论】:

  • 非常好的例子!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多