TsunamiCoder
TsunamiCoder

Reputation: 120

Close Wpf Popup On click of item of its own control template

I have a Wpf popup ,which has IsOpen property as

IsOpen="{Binding ElementName=GridItem,Path=IsMouseOver, Mode=OneWay, 
        UpdateSourceTrigger=PropertyChanged}"  

and also StaysOpen= True.

Inside the popup , I have a list box and each listboxitem contains a button as control template. I want to close the popup on clicking on the button but I am not able to close the popup on by clicking the button inside it.

Now if I use StaysOpen=False there are two different issues observed -

  1. While changing from one Grid item to other grid item, popup is not generated for newly hovered item.
  2. On left button down on Grid item , popup gets hidden.

My code snippet is as follows,

<ItemControl>
<DataTemplate>
<Grid>
<myControl x:Name="GridItem />
<popup IsOpen="{Binding ElementName=GridItem,Path=IsMouseOver, Mode=OneWay, 
UpdateSourceTrigger=PropertyChanged}" StaysOpen= True>
<Listbox ItemSource=xyz>
...
<ItemTemplate>
..
<DataTemplate>
<Button>
<!-- Clicking on button needs to close the popup-->
</Button>
</DataTemplate>
</ItemTemplate>
</ListBox>
</poup>
</Grid>
</DataTemplate>
</ItemControl>

Please help me in this regard.

Upvotes: 2

Views: 1501

Answers (1)

Carbine
Carbine

Reputation: 7903

Namespace

xmlns:i ="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"

Trigger with RelativeSource

<i:Interaction.Triggers>
    <i:EventTrigger EventName="Click">
        <ei:ChangePropertyAction 
        TargetObject="{Binding RelativeSource={RelativeSource AncestorType=Popup}}" 
        PropertyName="StaysOpen" Value="False"></ei:ChangePropertyAction>
        <ei:ChangePropertyAction
        TargetObject="{Binding RelativeSource={RelativeSource AncestorType=Popup}}" 
        PropertyName="IsOpen" Value="False"></ei:ChangePropertyAction>
    </i:EventTrigger>
</i:Interaction.Triggers>

Upvotes: 2

Related Questions