在制作充当拖放目标的自定义控件时,我需要将 AllowDrop
属性设置为 true
。我最初使用了以下代码,但发现 Drop
事件从未被触发:
EditorVisual.cs
public class EditorVisual : Control
{
static EditorVisual()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(EditorVisual),
new FrameworkPropertyMetadata(typeof(EditorVisual)));
AllowDropProperty.OverrideMetadata(typeof(EditorVisual),
new FrameworkPropertyMetadata(true));
}
// ...
protected override void OnDrop(DragEventArgs e)
{
base.OnDrop(e);
// this is never called
}
}
主题/Generic.xaml
<Style TargetType="{x:Type local:EditorVisual}">
<Setter Property="Background" Value="LightGreen" />
<!-- Uncomment to make things work -->
<!-- <Setter Property="AllowDrop" Value="True" /> -->
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:EditorVisual}">
<Border Background="{TemplateBinding Background}" />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
最终,我将范围缩小到设置 AllowDrop
属性的方式。在 xaml 或默认样式中设置它可以使一切正常工作......这让我感到困惑。为什么使用元数据覆盖不足以接收自定义控件中的拖/放事件?
编辑:在 Windows 8 上使用 Visual Studio 2012 和 .Net 4.0 在任何 CPU 上进行测试 - 调试。
最佳答案
静态构造函数不起作用的原因是 AllowDrop
是一个继承依赖属性。当您更改 EditorVisual
控件的默认值时,它不会将该值沿可视化树传播,这意味着您提供的模板中的控件仍将 AllowDrop
设置为假
。拖放过程中的 HitTest 遇到这些控件并失败。
使用样式来设置此值(或在控件的实例构造函数中)是完全可以的。
关于c# - OverrideMetadata 和默认样式的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19073989/