I have the following DataTemplate for my Tool Bar Items:
<DataTemplate DataType="{x:Type viewModels:PopupContextActionViewModel}">
<Grid>
<ToggleButton Name="ToggleButton">
<ContentControl Template="{Binding Icon, Converter={StaticResource NameToResourceConverter}}" Margin="5" />
</ToggleButton>
<Popup Name="ContextActionPopup" StaysOpen="False" AllowsTransparency="True"
IsOpen="{Binding
ElementName=ToggleButton,
Path=IsChecked,
Mode=TwoWay,
UpdateSourceTrigger=PropertyChanged}">
<Border Background="Transparent" Name="Border" Visibility="Visible">
<ContentControl x:Name="ContentControl" userInterface:RegionHelper.RegionName="{Binding RegionId}" Style="{StaticResource PopupContentStyle}" />
</Border>
</Popup>
</Grid>
<DataTemplate.Triggers>
<Trigger SourceName="ContentControl" Property="Content" Value="{x:Null}">
<Setter TargetName="ContextActionPopup" Property="IsOpen" Value="False" />
</Trigger>
</DataTemplate.Triggers>
Everything works fine (my popup and my toggle button do work together as they should) But if I set the value of the Trigger of my DataTemplate (which is done by my business logic, or more specific by some "NavigationService") the Popup gets closed while the ToggleButton stays checked.
Why does my Trigger not change the IsChecked Property of my ToggleButton as well?
Your answer can be found in the Dependency Property Value Precedence page on MSDN. In short, you have set a local value on the
IsOpen
property and that has a higher precedence than the value set by theTrigger
. The solution is to not set the local value, but to set the initial value in aStyle
instead, which has a lower precedence than theTrigger
.From the linked page on MSDN: