2

I'm trying to open a Popup with a button and have implemented a basic ICommand. The button is binded to the ICommand OpenPopupCommand while the Popup IsOpen attribute is binded to the "IsOpen" OnPropertyChanged. My thought process was to bind the Popup.IsOpen attribute to the ICommand as well to have IT trigger the OnPropertyChange but couldn't get it to work. I think I'm close but can't figure it out. Here is the code I have so far:

    #region ICommand Members

    private ICommand _openPopupCommand;
    public ICommand OpenPopupCommand
    {
        get
        {
            if (_openPopupCommand == null)
                _openPopupCommand = new RelayCommand(param => OpenPopupExecute(param));
            return _openPopupCommand;
        }
        set 
        {
            _openPopupCommand = value;
        }
    }

    #endregion

    #region Methods

    public void OpenPopupExecute(object parameter)
    {
        parameter = true;
        OnPropertyChanged("IsOpen");
    }

    #endregion

Button that "pops up" the Popup and the Popup XAML:

<Popup x:Name="FieldsPopup" Placement="Center" Width="400" Height="250" IsOpen="{Binding IsOpen}">
                    <StackPanel>
                        <TextBlock Background="LightBlue" HorizontalAlignment="Center" VerticalAlignment="Center" Height="250" Width="350" TextAlignment="Center" >This is a popup</TextBlock>
                    </StackPanel>
                </Popup>
<Button Name="button_PatientIdentifierList" Width="23" Height="23" Grid.Column="2" Foreground="Black" Background="#FFCDCDCD" BorderBrush="#FF707070" Margin="3.4,4,4,0" VerticalAlignment="Top" Command="{Binding OpenPopupCommand}"/>
jarheadWill
  • 63
  • 1
  • 1
  • 7
  • 1
    I think it would be valuable if you gave a bit more context in terms of code. – Paymahn Moghadasian Jul 25 '14 at 20:30
  • What exactly are trying to say with *"Popup IsOpen attribute is binded to the "IsOpen" OnPropertyChanged"*? It sounds like you want to say that you made a binding between a dependency property and a method -- which is not possible and does not make sense. (Also note that Popup.IsOpen is **NOT** an attribute, it is a dependency property. Attributes are something else entirely...) –  Jul 25 '14 at 20:54
  • please post you xaml too. – pushpraj Jul 25 '14 at 23:35
  • Your code looks OK though incomplete, but it is impossible for us to tell without your xaml markup. I Hope OnPropertyChanged("IsOpen") raises the event in the INotifyPropertyChanged interface, but we are guessing here =). Complete code and xaml please. – Stígandr Jul 26 '14 at 07:48
  • @User1489726 Sorry bout that.. – jarheadWill Jul 28 '14 at 18:40
  • @elgonzo thank you for the correction. I updated the post with the XAML. – jarheadWill Jul 28 '14 at 18:41
  • @Stian Thanks, here is my XAML. – jarheadWill Jul 28 '14 at 18:41
  • @WillTheThrill, `OnPropertyChanged("IsOpen");` sounds like your class (which implements the *OpenPopupExecute* methdo) notifies that **its** *IsOpen* property is being changed. Does your class has such an *IsOpen* property implemented? If so, i do not see any code in your qeustion that would change its value from *false* to *true*... Also, make sure that the DataContext used by the XAML in your question is in fact an object of your class... –  Jul 28 '14 at 18:46
  • Could you post the complete working code for this? – D. Rattansingh Aug 18 '15 at 17:21

1 Answers1

3

You're raising the PropertyChange notification, but I don't see you actually changing the property anywhere.

Unless I'm mistaken, this code here takes the CommandParameter (called parameter here) and sets it to true

public void OpenPopupExecute(object parameter)
{
    parameter = true;
    OnPropertyChanged("IsOpen");
}

However in your XAML the Button.CommandParameter isn't bound to anything

<Button Command="{Binding OpenPopupCommand}"/>

So I suspect that parameter is just null, and is not actually doing anything here.

What you seem to be missing is the actual IsOpen property definition, and setting it to true in your command's Execute code :

private bool _isOpen;
public bool IsOpen
{
    get
    {
        return _isOpen;
    }
    set 
    {
        _isOpen = value;
        OnPropertyChanged("IsOpen");
    }
}

public void OpenPopupExecute(object parameter)
{
    IsOpen = true; // Will call OnPropertyChanged in setter
}

As a side note, I really don't like WPF's default PopupControl, and have a custom UserControl version of it on my blog if you ever decide you hate WPF's default PopupControl too :)

Rachel
  • 130,264
  • 66
  • 304
  • 490
  • `parameter` is an argument variable of the *OpenPopupExecute* mMethod, and thus local to the method. Assigning a different value to that variable does not have any effect outside of that method... Thus, your musings about CommandParameter are unfounded. Anyway, i am just nitpicking. The other and more important part about IsOpen is ofcourse sound and true. (Okay, some more nitpicking: It would be nice to safeguard the setter so that the event is not raised if the property is set to a value it already has :) ) –  Jul 28 '14 at 21:03
  • @Rachel I appreciate the customer UserControl version of your PopupControl! I'm a bit of a rookie (if you couldn't tell) and likely get lost but I'll take a look! Also, I had something similar to the solution you posted with the only difference being the _OpenPopupExectue_ body not having the _IsOpen = true;_ part. I made the change and we have success! Thank you all for the help! – jarheadWill Jul 28 '14 at 21:59
  • @Rachel I agree the default popup control is not very nice to work with, and I've done the same as you. It's not part of the visual tree, and tons of issues with the control. Particulary when it comes to XBAP applications... – Stígandr Jul 29 '14 at 16:27