Jatinder Walia
Jatinder Walia

Reputation: 141

wpf popup not showing on button click

I have a button and a popup control in it in my xaml as follows:

<Button Grid.Column="0" Grid.Row="5" 
        HorizontalAlignment="Center" 
        Margin="88,8,214,0"  
        Grid.RowSpan="2" Height="26" 
        VerticalAlignment="Top" Width="22"
        IsEnabled="{Binding Path=SearchFound}" 
        x:Name="cmdPlanList" 
        Click="cmdPlanList_Click">
    <ContentControl>
        <Popup IsOpen = "{Binding PlanPopup}"
               PlacementTarget = "{Binding ElementName = cmdPlanList}"
               AllowsTransparency = "True" 
               PopupAnimation = "Slide" 
               x:Name="Popup4Plan">
            <Canvas Width = "125" 
                    Height = "100" 
                    Background = "Red" >
                <Canvas.RenderTransform>
                    <RotateTransform x:Name = "theTransform" />
                </Canvas.RenderTransform>
                <TextBlock TextWrapping = "Wrap" 
                           Foreground = "Blue"  
                           Text = "Hi, this is Popup" />
            </Canvas>
        </Popup>
    </ContentControl>
</Button>

I am setting the DataContext of this Popup from my code-behind as follows:-

My View's code behind:-

using xyz
{ 
    private bool _PlanPopup = false;
    public bool PlanPopup
    {
        get { return _PlanPopup; }
        set
        {
            _PlanPopup = value;
        }
    }
    public MyView()
    {
        InitializeComponent();
        Popup4Plan.DataContext = this;
    }
    private void cmdPlanList_Click(object sender, RoutedEventArgs e)
    {
        this.PlanPopup = this.PlanPopup ? false : true;
    }
}

Upvotes: 0

Views: 1697

Answers (2)

Emond
Emond

Reputation: 50692

If you want to bind the View to a property of itself, make the property a dependency property.

public bool IsOpen
{
    get
    {
        return (bool)GetValue(IsOpenProperty);
    }
    set
    {
        SetValue(IsOpenProperty, value);
    }
}

public static readonly DependencyProperty IsOpenProperty =
    DependencyProperty.Register("IsOpen", typeof(bool), typeof(MyView), new PropertyMetadata(false));

To quickly make it type propdp [tab][tab] and fill in the blanks.

Also:

this.PlanPopup = this.PlanPopup ? false : true;

looks much better this way:

this.PlanPopup = !this.PlanPopup;

Upvotes: 2

woelliJ
woelliJ

Reputation: 1504

For updates on bound properties to work you need to implement INotifyPropertyChanged. For example like the following.

public class XYZ : INotifyPropertyChanged
{

    private bool isOpen;

    public bool IsOpen {
        get { return this.isOpen; }
        set {
            this.isOpen = value;
            this.OnPropertyChanged();
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

}

With this code an instance of XYZ will notify that the property IsOpen has changed and any bound view elements will re-fetch the value of IsOpen.

Upvotes: 0

Related Questions