使用MVVM在WPF中显示弹出窗口 [英] Displaying Popup in WPF using MVVM

查看:1016
本文介绍了使用MVVM在WPF中显示弹出窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hi I am using MVVM first time, and worried about displaying Popup control of my XAML using ViewModel, My XMAL is as follows-

 <Button Content="Add Customer"  Name = "AddButton" Command="{Binding ShowPopup, UpdateSourceTrigger=PropertyChanged }"/>

  Popup Name="TestPopUp" IsOpen="False"
   StackPanel Margin="0,0,0,30"
    TextBlock Text="First Name:" FontWeight="Bold" Margin="0,0,5,0"
     TextBlock Text="{Binding FirstName}" MinWidth="50" Margin="0,0,5,0"
     TextBlock Text="Last Name:" FontWeight="Bold" Margin="0,0,5,0"
     TextBlock Text="{Binding LastName}" MinWidth="50" Margin="0,0,5,0"
     TextBlock Text="Age:" FontWeight="Bold" Margin="0,0,5,0"
     TextBlock Text="{Binding Age}" MinWidth ="150"  Margin="0,0,5,0"
     Button Content="Add Customer" Command="{Binding AddCustomerCommand}"Height="27" MinWidth ="200" Width="200"
                StackPanel
            Popup

My view Model Code:-
 public class MainViewModel : ViewModelBase
    {

        private DelegateCommand<object> _showpopup;

        private void DisplayPopup(object param)
        {

            MessageBox.Show("Pop up need to be open Here");

        }


   public DelegateCommand<object> ShowPopup
        {
            get
            {
                if (_showpopup  == null)
                {
                    _showpopup = new DelegateCommand<object>(DisplayPopup, CanAddCustomer);
                }
                return _showpopup;
            }
        }

the alert Massage is showing correctly , but I wat to open the Popup window instead. Please help.

推荐答案

在MVVM中实现此目标的标准方法实际上非常简单.您需要做的就是在您的ViewModel中添加一个布尔属性,将IsOpen属性绑定到您的Popup上.当您发出更改通知时,弹出窗口应该做出适当的反应(只要您没有更改默认更新触发器).这是您需要添加到ViewModel中的内容(假设您的VM使用RaisePropertyChanged作为属性通知的方法名称):
The standard way to achieve this in MVVM is actually very straightforward. All you need to do is add a boolean property to your ViewModel that you bind the IsOpen property to on your Popup. When you raise the change notification, the Popup should react appropriately (as long as you haven''t changed the default update trigger). Here''s what you need to add to your ViewModel (assuming that your VM uses RaisePropertyChanged as the method name for property notification):
private bool _isOpen;
public bool IsOpen
{
  get { return _isOpen; }
  set
  {
    if (_isOpen == value) return;
    _isOpen = value;
    RaisePropertyChanged("IsOpen");
  }
}

在XAML的Popup声明中,添加以下属性

In your Popup declaration in the XAML, add the following attribute

IsOpen="{Binding IsOpen}"

就这样-MVVM友好的显示方式弹出窗口.

And that''s it - an MVVM friendly way to show the popup.


这篇关于使用MVVM在WPF中显示弹出窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆