UIElement.AllowDrop Property


获取或设置一个值,该值指示此元素能否用作拖放操作的目标。Gets or sets a value indicating whether this element can be used as the target of a drag-and-drop operation. 这是依赖项属性。This is a dependency property.

 property bool AllowDrop { bool get(); void set(bool value); };
public bool AllowDrop { get; set; }
member this.AllowDrop : bool with get, set
Public Property AllowDrop As Boolean

Property Value


如果此元素可用作拖放操作的目标,则为 true;否则为 falsetrue if this element can be used as the target of a drag-and-drop operation; otherwise, false. 默认值为 falseThe default value is false.


以下标记示例使用 TextBox上的属性设置 true AllowDrop 属性,并设置聚合中的某些其他相关属性,使该 TextBox 在拖动时成为多行文本数据对象的目标。The following markup example sets the AllowDrop property true using an attribute on a TextBox, as well as setting some other related properties that in aggregate enable that TextBox to be the target of a multiline text data object when it is dragged in. 有关完整示例,请参阅加载已删除的文件示例For the complete sample, see Load a Dropped File Sample.

  AcceptsReturn="True" AcceptsTab="True" 
  BorderThickness="1" BorderBrush="Black" 
  HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto"
  PreviewDragOver="ehDragOver" PreviewDrop="ehDrop"


默认情况下不启用拖放操作,并且必须通过将 AllowDrop 设置为 true来特意启用。Drag-and-drop operations are not enabled by default, and must be enabled deliberately by setting AllowDrop to true. 除了此基本设置外,拖放行为完全是特定实现的,并且不是由 UIElement 或任何其他基元素类定义的。Beyond this basic setting, drag-and-drop behavior is entirely implementation specific and is not defined by UIElement or any other base element class. 某些控件(例如 RichTextBox)具有默认行为。Certain controls, for example, RichTextBox, do have a default behavior. 有关拖放的详细信息,请参阅拖放概述For more information on drag and drop, see Drag and Drop Overview.

FrameworkElement 在其实现中重写此依赖项属性的元数据。FrameworkElement overrides the metadata for this dependency property in its implementation. 具体而言,FrameworkElement 指定此属性以允许属性值继承(Inherits 在元数据中 true)。Specifically, FrameworkElement designates this property to allow property value inheritance (Inherits is true in metadata). 此上下文中的属性值继承表示,如果有子元素对于通过本地值或样式分配的 AllowDrop 没有其他值,则为已分配此值的最近父元素的值(同样,在样式中、默认值或本地值),并将父元素的值分配给属性系统以前未分配的所有子元素。Property value inheritance in this context means that if there are child elements with no other value for AllowDrop assigned through local values or styles, the value of the nearest parent element with this value assigned (again, either in styles, by default values, or a local value), then the value from the parent element will be assigned to all previously unassigned child elements by the property system. 在实际操作中,这意味着你可以指定是否允许在根元素处放置操作,并且该值将传播到尚未专门分配给 false的所有子元素。In practice this means that you can specify whether to allow drop operations at the root element, and that value will propagate to all child elements that have not specifically assigned it as false.

依赖项属性信息Dependency Property Information

标识符字段Identifier field AllowDropProperty
元数据属性设置为 trueMetadata properties set to true NoneNone

Applies to