模板中控件之间的绑定

发布于 2024-12-13 11:45:50 字数 2426 浏览 0 评论 0原文

<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>

我定义了以下样式。我想将 Popup 的 IsOpen 属性绑定到 ToggleButton 的 IsChecked 属性。

我试图使用 FindAncestor 来查找网格并从那里开始工作,但我无法做到正确。有没有办法绑定这两个对象?如果是这样怎么办?

<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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

携君以终年 2024-12-20 11:45:50

这应该有效:

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

This should work:

IsOpen="{Binding ElementName=Bd, Path=IsChecked}"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文