CheckBox.Content
是ToggleButton
以及 Popup
。当ToggleButton
为Checked
时,弹出广告会显示。在 Popup
中,有三个元素。如果我点击一个元素, Popup
应该关闭。
重现问题的步骤:
这是我的代码。
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<CheckBox>
<DockPanel Panel.ZIndex="2" HorizontalAlignment="Right">
<Popup Name="facePopup" DockPanel.Dock="Bottom" StaysOpen="True" AllowsTransparency="True"
Placement="Bottom" VerticalOffset="1"
IsOpen="{Binding Path=IsChecked, ElementName=roleFaceButton}" MouseUp="facePopup_MouseUp" >
<StackPanel>
<Ellipse Fill="Red" Width="18" Height="25"/>
<Ellipse Fill="Green" Width="18" Height="25"/>
<Ellipse Fill="Blue" Width="18" Height="25"/>
</StackPanel>
</Popup>
<ToggleButton x:Name="roleFaceButton">
<Ellipse Fill="Black" Width="18" Height="25"/>
</ToggleButton>
</DockPanel>
</CheckBox>
</Window>
和代码隐藏
private void facePopup_MouseUp(object sender, MouseButtonEventArgs e)
{
facePopup.IsOpen = false;
}
我发现如果我用 Border 替换CheckBox
,我的代码就可以了。为什么呢?
答案 0 :(得分:1)
I find that if I substitute CheckBox by Border, my code will work. Why?
据我所知,您无法在复选框中添加鼠标向上或向下事件,因为Checked事件会占用它们。由于边框没有默认行为,因此可以添加鼠标事件。您可以尝试使用PreviewMouseLeftButtonDown
,它应该正常启动