WPF开发为按钮提供添加,删除和重新排列ListBox内容的功能。
该行为实际上有四个独立的部分,可以在一个类中执行不同的功能:
添加项目,将所选项目向上移动一个位置
将所选项目向下移动一个位置,删除所选项目。
每个函数的代码结构非常相似,只有一些细节不同。
代码:定义DependencyProperty:
public static readonly DependencyProperty MoveItemUpProperty =
DependencyProperty.RegisterAttached("MoveItemUp",
typeof(Selector), typeof(ListHelperBehavior),
new PropertyMetadata(null, OnMoveItemUpChanged));
public static Selector GetMoveItemUp(UIElement uiElement)
{ return (Selector)uiElement.GetValue(MoveItemUpProperty); }
public static void SetMoveItemUp(UIElement uiElement, Selector value)
{ uiElement.SetValue(MoveItemUpProperty, value); }
用于为包含列表的Selector(或ListBox)控件提供绑定。它用于Button执行动作,在这种情况下是将所选项目向上移动一个位置。对于这个动作的代码需要有机会获得ItemsSource