问题
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 -
- While changing from one Grid item to other grid item, popup is not generated for newly hovered item.
- 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.
回答1:
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>
来源:https://stackoverflow.com/questions/22470490/close-wpf-popup-on-click-of-item-of-its-own-control-template