Nathanael
Nathanael

Reputation: 1772

Binding between controls in a template

<Style x:Key="FavouriteMenuItemStyle" TargetType="{x:Type MenuItem}" BasedOn="{StaticResource BasicFavouriteItemStyle}">
    <Setter Property="Width" Value="Auto"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type MenuItem}">
                <Grid>
                    <ToggleButton x:Name="Bd"
                                  Content="{Binding Header}"
                                  Style="{StaticResource FolderButtonStyle}"
                                  Height="{TemplateBinding Height}"
                                  Width="{TemplateBinding Width}"
                                  Margin="{TemplateBinding Margin}"
                                  Padding="{TemplateBinding Padding}"
                                  Focusable="False"/>

                    <Popup x:Name="PopupMenu" 
                           IsOpen="False" 
                           Placement="Bottom"
                           PopupAnimation="{DynamicResource {x:Static SystemParameters.MenuPopupAnimationKey}}" 
                           AllowsTransparency="True"
                           Focusable="False"
                           StaysOpen="False">

                        <Border BorderBrush="{StaticResource MpButtonNormalStrokeBrush}" 
                                Background="{StaticResource MpButtonNormalFillBrush}" 
                                BorderThickness="1"
                                CornerRadius="3"
                                Padding="4">
                            <ItemsControl ItemsSource="{Binding Favourites}" ItemTemplate="{StaticResource FavouriteMenuItemDataTemplate}"/>
                        </Border>
                    </Popup>
                </Grid>

                <ControlTemplate.Triggers>
                    <Trigger Property="IsChecked" SourceName="Bd" Value="True">
                        <Setter Property="IsOpen" TargetName="PopupMenu" Value="True"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

I have the following style defined. I would like to bind the Popup's IsOpen property to the ToggleButton's IsChecked property.

I was trying to use FindAncestor to find the grid and work from there, but I wasn't able to get it right. Is there a way to bind these two objects? If so how?

Upvotes: 2

Views: 132

Answers (1)

Mark Synowiec
Mark Synowiec

Reputation: 5445

This should work:

IsOpen="{Binding ElementName=Bd, Path=IsChecked}"

Upvotes: 3

Related Questions