HandyControl
HandyControl copied to clipboard
[Feature request] SplitButton toggle DropDown open
trafficstars
HandiControl 提供了 ContextMenuToggleButton,它能通过鼠标左键切换打开菜单,SplitButton 也支持打开下拉项(“菜单”),但是它的行为类似于平时的右键菜单行为,不论 DropDown 是否打开,右键都将重新打开 DropDown,我希望当 DropDown 打开时,再次点击鼠标左键,将收起 DropDown。
现在的临时解决方法是这样:
HandiControl provides
ContextMenuToggleButton, which can open the menu by left mouse button toggle,SplitButtonalso supports opening dropdown items ("menu"), but its behavior is similar to the usual right-click menu behavior, no matterDropDownis open or not, right-clicking will re-openDropDown, and I want that whenDropDownis open, clicking the left mouse button again will put awayDropDown. The temporary workaround now is this:
public class SplitButtonToggleDropDownBehavior : Behavior<SplitButton>
{
protected override void OnAttached()
{
base.OnAttached();
AssociatedObject.PreviewMouseLeftButtonDown += AssociatedObject_PreviewMouseLeftButtonDown;
}
protected override void OnDetaching()
{
AssociatedObject.PreviewMouseLeftButtonDown -= AssociatedObject_PreviewMouseLeftButtonDown;
base.OnDetaching();
}
private void AssociatedObject_PreviewMouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
var result = VisualTreeHelper.HitTest(AssociatedObject, e.GetPosition(AssociatedObject));
// 如果 VisualHit 为 Border 或 Path,则鼠标在点击 SplitButton 的 DropDown 位置,
// If VisualHit is Border or Path, the mouse is clicking on the DropDown position of the SplitButton.
// 如果为 TextBlock,则鼠标在点击 SplitButton 的非 DropDown 位置。
// If TextBlock, the mouse is clicking on the non-DropDown position of the SplitButton.
// 如果为 null,则鼠标在点击其他位置。
// If it is null, then the mouse is clicking on other position.
// 如果不判断 VisualHit,则在点击下拉项前,此事件将被调用,下拉项被关闭,导致下拉项无法被点击到。
// If VisualHit is not judged, this event will be called before the dropdown item is clicked and the dropdown item is closed, causing the dropdown item not to be clicked to.
if (AssociatedObject.IsDropDownOpen && result?.VisualHit is Border or Path or TextBlock)
{
AssociatedObject.IsDropDownOpen = false;
e.Handled = true;
}
}
}
所以能否提供一个属性 IsToggleDropDownOpen?
So could you provide a property
IsToggleDropDownOpen?