C# 带有弹出窗口的按钮
我有一个按钮,当用户把鼠标放在元素上时,我想显示一个弹出窗口。 在此弹出窗口中,包含用户可以单击的按钮。 所有这些都在Itemscontrol中 当用户将鼠标放在按钮上时,我已经显示了弹出窗口,但我不知道如何: 1) 如果用户没有“关注”弹出窗口,则隐藏弹出窗口 2) 如果用户关注弹出窗口,则在关注按钮后,弹出窗口必须保持打开状态 我现在的代码:C# 带有弹出窗口的按钮,c#,wpf,mvvm,popup,caliburn.micro,C#,Wpf,Mvvm,Popup,Caliburn.micro,我有一个按钮,当用户把鼠标放在元素上时,我想显示一个弹出窗口。 在此弹出窗口中,包含用户可以单击的按钮。 所有这些都在Itemscontrol中 当用户将鼠标放在按钮上时,我已经显示了弹出窗口,但我不知道如何: 1) 如果用户没有“关注”弹出窗口,则隐藏弹出窗口 2) 如果用户关注弹出窗口,则在关注按钮后,弹出窗口必须保持打开状态 我现在的代码: <Button x:Name="MyButton" MouseEnter="LabelShift_MouseDown" Background
<Button x:Name="MyButton" MouseEnter="LabelShift_MouseDown" Background="{x:Null}" BorderBrush="{x:Null}" Style="{DynamicResource SquareButtonStyle}" >
<Grid MaxHeight="80">
<Grid.RowDefinitions>
<RowDefinition Height="3*"/>
<RowDefinition Height="2*"/>
</Grid.RowDefinitions>
<Viewbox Grid.Row="0" >
<TextBlock Name="Identifier" FontSize="26" Margin="10,10,10,10" Foreground="White" Text="{Binding Identifier}"/>
</Viewbox>
<Viewbox Grid.Row="1" VerticalAlignment="Bottom">
<TextBlock Name="Value" FontSize="24" Margin="10,10,10,10" Foreground="White" Text="{Binding Total, StringFormat='C'}"/>
</Viewbox>
</Grid>
<Button.Triggers>
<EventTrigger RoutedEvent="MouseEnter">
<BeginStoryboard>
<Storyboard>
<BooleanAnimationUsingKeyFrames
Storyboard.TargetName="ToolTip"
Storyboard.TargetProperty="IsOpen">
<DiscreteBooleanKeyFrame
KeyTime="00:00:00"
Value="True" />
</BooleanAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
<EventTrigger RoutedEvent="MouseLeave">
<BeginStoryboard>
<Storyboard>
<BooleanAnimationUsingKeyFrames
Storyboard.TargetName="ToolTip"
Storyboard.TargetProperty="IsOpen">
<DiscreteBooleanKeyFrame
KeyTime="00:00:00"
Value="False" />
</BooleanAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Button.Triggers>
</Button>
<Popup x:Name="ToolTip" PlacementTarget="{Binding ElementName=MyButton}" StaysOpen="True" Placement="Bottom" Height="Auto" Width="{Binding ActualWidth, ElementName=MyButton}" AllowsTransparency="True">
<Grid>
<Border BorderBrush="#909090" BorderThickness="1" CornerRadius="1" >
<StackPanel Margin="10">
<!-- Content/Elements-->
</StackPanel>
</Border>
</Grid>
<Popup.Triggers>
<EventTrigger RoutedEvent="MouseEnter">
<BeginStoryboard>
<Storyboard>
<BooleanAnimationUsingKeyFrames
Storyboard.TargetName="ToolTip"
Storyboard.TargetProperty="IsOpen">
<DiscreteBooleanKeyFrame
KeyTime="00:00:00"
Value="True" />
</BooleanAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Popup.Triggers>
下面是一个简单的示例,说明如何实现下拉功能。我之所以分享这一点是因为: a) 它继承了ToggleButton,提供了我们需要的所有功能 b) 小代码 c) 它起作用了
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
namespace Controls
{
public class DropDownButton : System.Windows.Controls.Primitives.ToggleButton
{
public static readonly DependencyProperty DropDownProperty = DependencyProperty.Register("DropDown", typeof(ContextMenu), typeof(DropDownButton), new UIPropertyMetadata(null));
public ContextMenu DropDown
{
get
{
return (ContextMenu)GetValue(DropDownProperty);
}
set
{
SetValue(DropDownProperty, value);
}
}
public DropDownButton()
{
// Bind the ToogleButton.IsChecked property to the drop-down's IsOpen property
Binding binding = new Binding("DropDown.IsOpen");
binding.Source = this;
this.SetBinding(IsCheckedProperty, binding);
}
protected override void OnClick()
{
if (DropDown != null)
{
//If there is a drop-down assigned to this button, then position and display it
DropDown.PlacementTarget = this;
DropDown.Placement = PlacementMode.Bottom;
DropDown.IsOpen = true;
}
}
}
}
如果您希望使用UserControl来代替ContextMenu,只需向所述控件添加一个名为IsOpen的新属性,并遵循与上述示例相同的逻辑,只有在选中ToggleButton时,才能使用绑定来显示UserControl。如果有标准的工具提示,则工具提示不是此自定义鼠标输入的好名称。如果用户打开下拉列表,然后单击按钮将其关闭,该行为如何?我发现在这种情况下,
ToggleButton
的行为很奇怪,因此我最终使用了一个绑定到简单的OpenCommand
的普通按钮,并在您希望删除绑定时禁用该按钮。然后单击,如果打开,则将其关闭。如果它关闭了,打开它<代码>如果(DropDown!=null&&DropDown.IsOpen)DropDown.IsOpen=false;else if(DropDown!=null&DropDown.IsClosed)DropDown.IsOpen=true代码>。检查ToggleButton是否被选中也是一个选项。因此,您只是使用一个按钮来显示视觉效果。或者/or。我发现切换按钮更适合我的情况。问题是,我想这家伙希望它在悬停时打开,而不是单击。我无法理解他的要求。悬停弹出窗口激怒了我,但我不想就此和他争论。祝你好运