UIElement.AllowDrop 属性

定义

获取或设置一个值,该值指示此元素能否用作拖放操作的目标。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.

public:
 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

属性值

Boolean

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

示例

以下标记示例 AllowDrop true 使用上的属性设置属性 TextBox ,并设置聚合中的某些其他相关属性,使其能够在 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.

<TextBox
  Name="tbDisplayFileContents" 
  Grid.Row="1" 
  AcceptsReturn="True" AcceptsTab="True" 
  AllowDrop="True" 
  BorderThickness="1" BorderBrush="Black" 
  HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto"
  
  PreviewDragOver="ehDragOver" PreviewDrop="ehDrop"
/>

注解

默认情况下不启用拖放操作,并且必须通过将设置 AllowDrop 为来特意启用 trueDrag-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. 在实践中,这意味着你可以指定是否允许在根元素处放置操作,并且该值将传播到尚未专门将其分配为的所有子元素 falseIn 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 None

适用于