2

抱歉,标题含糊不清,我正在开发一个 WPF 项目,而且它变得相当烦人。我知道 VS 设计师有时有点挑剔,但希望这是我能解决的问题。

我有一个依赖属性,我也在绑定,但是设计师给了我蓝色曲线和一个错误:

Error 13 A 'Binding' cannot be used within a 'TextBlock' collection. A 'Binding' can only be set on a DependencyProperty of a DependencyObject.

但是,当我运行该应用程序时,一切正常,没有绑定错误,一切都按预期工作。VS自从第一次发生以来已经重启了很多次,现在仍然发生。

我看不出它所指的 DependancyProperty 有什么问题,对我来说一切看起来都很标准,但也许你们中的一个人可以解释一下(希望如此)。我不记得我从哪里得到 DP 的代码,我知道它是在线的,但我已经稍微调整了一下(我认为)。

运行 VS2010,项目的目标是 .net4.0(不是客户端配置文件)。

谢谢!

XAML

<TextBlock Grid.Column="1" Grid.Row="0" AllowDrop="True" behaviours:DropBehavior.PreviewDropCommand="{Binding Path=DropFile}" Style="{StaticResource styFile}">

DP

public static class DropBehavior {

    private static readonly DependencyProperty PreviewDropCommandProperty = DependencyProperty.RegisterAttached(
        "PreviewDropCommand",
        typeof(ICommand),
        typeof(DropBehavior),
        new PropertyMetadata(null, PreviewDropCommandPropertyChangedCallBack)
    );

    public static void SetPreviewDropCommand(this UIElement inUIElement, ICommand inCommand) {
        inUIElement.SetValue(PreviewDropCommandProperty, inCommand);
    }

    private static ICommand GetPreviewDropCommand(UIElement inUIElement) {
        return (ICommand)inUIElement.GetValue(PreviewDropCommandProperty);
    }

    private static void PreviewDropCommandPropertyChangedCallBack(
        DependencyObject inDependencyObject, DependencyPropertyChangedEventArgs inEventArgs) {
        UIElement uiElement = inDependencyObject as UIElement;
        if (null == uiElement)
            return;

        uiElement.Drop += (sender, args) => {
            GetPreviewDropCommand(uiElement).Execute(args.Data);
            args.Handled = true;
        };
    }
}
4

1 回答 1

2

在对 UI 抱怨了很多之后,又重新审视了这个问题,原来是这里的这一行:

private static readonly DependencyProperty PreviewDropCommandProperty = DependencyProperty.RegisterAttached(
    "PreviewDropCommand",
    typeof(ICommand),
    typeof(DropBehavior),
    new PropertyMetadata(null, PreviewDropCommandPropertyChangedCallBack)
);

它应该是一个public,而不是private声明。很好奇应用程序运行良好,只是不是设计师(或者如果我知道 VS 的内部工作原理,也许不是那么好奇)

于 2013-06-18T07:42:21.190 回答