模板中控件之间的绑定
<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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这应该有效:
This should work: