Click here to Skip to main content
15,893,904 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
XML
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.
Posted

1 solution

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):
C#
private bool _isOpen;
public bool IsOpen
{
  get { return _isOpen; }
  set
  {
    if (_isOpen == value) return;
    _isOpen = value;
    RaisePropertyChanged("IsOpen");
  }
}
In your Popup declaration in the XAML, add the following attribute
XML
IsOpen="{Binding IsOpen}"
And that's it - an MVVM friendly way to show the popup.
 
Share this answer
 
Comments
vinod deo from navi mumbai 15-Jul-11 4:40am    
Thanks It is working fine now !
Pete O'Hanlon 15-Jul-11 4:43am    
You're welcome. I'm glad it helped.
MrNilesh 7-Dec-11 5:20am    
where to put IsOpen propery?, can you share the code.
Thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900