【发布时间】:2011-12-07 00:26:50
【问题描述】:
【问题讨论】:
标签: c# .net wpf xaml wpf-controls
【问题讨论】:
标签: c# .net wpf xaml wpf-controls
我使用这个类,它看起来很棒: http://andyonwpf.blogspot.com/2006/10/dropdownbuttons-in-wpf.html
我将在这里发布,以便阅读更清晰:
/// <summary>
/// Andy On WPF: DropDownButtons in WPF
/// http://andyonwpf.blogspot.com/2006/10/dropdownbuttons-in-wpf.html
/// </summary>
public class DropDownButton : ToggleButton
{
#region Members
public enum Placement { Bottom, Right }
#endregion
#region Properties
#region DropDownPlacement
/// <summary>
/// DropDown placement.
/// </summary>
public Placement DropDownPlacement
{
get { return (Placement)GetValue(DropDownPlacementProperty); }
set { SetValue(DropDownPlacementProperty, value); }
}
/// <summary>
/// DropDown placement (Dependency Property).
/// </summary>
public static readonly DependencyProperty DropDownPlacementProperty =
DependencyProperty.Register("DropDownPlacement", typeof(Placement),
typeof(DropDownButton), new UIPropertyMetadata(null));
#endregion
#region DropDown
/// <summary>
/// DropDown property.
/// </summary>
public ContextMenu DropDown
{
get { return (ContextMenu)GetValue(DropDownProperty); }
set { SetValue(DropDownProperty, value); }
}
/// <summary>
/// DropDown property (Dependency property).
/// </summary>
public static readonly DependencyProperty DropDownProperty =
DependencyProperty.Register("DropDown", typeof(ContextMenu),
typeof(DropDownButton), new PropertyMetadata(null, OnDropDownChanged));
#endregion
#endregion
#region Events
private static void OnDropDownChanged(DependencyObject sender,
DependencyPropertyChangedEventArgs e)
{
((DropDownButton)sender).OnDropDownChanged(e);
}
void OnDropDownChanged(DependencyPropertyChangedEventArgs e)
{
if (DropDown != null)
{
DropDown.PlacementTarget = this;
switch (DropDownPlacement)
{
default:
case Placement.Bottom:
DropDown.Placement = PlacementMode.Bottom;
break;
case Placement.Right:
DropDown.Placement = PlacementMode.Right;
break;
}
this.Checked +=
new RoutedEventHandler((a, b) => { DropDown.IsOpen = true; });
this.Unchecked +=
new RoutedEventHandler((a, b) => { DropDown.IsOpen = false; });
DropDown.Closed +=
new RoutedEventHandler((a, b) => { this.IsChecked = false; });
}
}
#endregion
}
【讨论】:
它有一个自定义的control template,它看起来有一个透明的背景,除非鼠标悬停,当然渐变和边框也不同。
在 MSDN 上有 an example 用于自定义模板,它会导致一个相当蓝色的按钮,基本上你可以对模板做任何事情,但它们的创建可能是相当多的工作。 Expression Blend 有助于控制模板。
【讨论】:
Menu 和MenuItem,因为的功能。如果您知道如何创建一个包含状态、PART 等知识的模板。您应该能够重新创建它,这并不像您需要忠实于 Windows 中使用的实际内部结构。
要获取下拉菜单部分,您可以设置按钮的 ContextMenu 属性,然后使用 ContextMenu.Placement 将其正确定位在按钮下方。
您可能还必须设置 ContextMenu.PlacementTarget 以使其相对于 Button。
【讨论】:
抱歉再次添加,但我刚刚意识到这个控件也存在于 Codeplex 上的扩展 WPF 工具包中:
http://wpftoolkit.codeplex.com/wikipage?title=DropDownButton
它是这样实现的(根据他们的网站):
<extToolkit:DropDownButton Content="Click Me" Margin="15" >
<extToolkit:DropDownButton.DropDownContent>
<extToolkit:ColorCanvas />
</extToolkit:DropDownButton.DropDownContent>
</extToolkit:DropDownButton>
您可以根据需要在其中添加 MenuItems。
我已经将此套件用于其他功能(ChildWindow 和 SplitButton),我认为它做得很好。随着人们不断要求 WPF 中提供更多 Office 2007 / 2010 功能,CodePlex 和 Microsoft 会继续期待更多这样的功能。
【讨论】: