【问题标题】:'True' is not a valid value for property 'IsDragSource'“True”不是属性“IsDragSource”的有效值
【发布时间】:2022-03-30 21:01:01
【问题描述】:

我对 WPF 依赖属性有一个奇怪的问题:

/// <summary>
/// attached property that defines if the source is a drag source
/// </summary>
public static readonly DependencyProperty IsDragSourceProperty =
    DependencyProperty.RegisterAttached("IsDragSource", 
        typeof(bool?), 
        typeof(DragDropBehaviour),
        new PropertyMetadata(null, 
            new IsDragSourceBehaviour().PropertyChangedHandler));

当我尝试在我的 XAML 窗口中使用它时,它给了我卷线。

<ListBox IsSynchronizedWithCurrentItem="True"
         AllowDrop="False"
         ItemsSource="{Binding Objects, Mode=TwoWay}" 
         i:DragDropBehaviour.DragDropHandler="{Binding}"
         i:DragDropBehaviour.IsDragSource="True" Grid.Column="0"/>

...声明“True”不是属性“IsDragSource”的有效值。 True 应该是类型 (bool?) 的 Dependency Property 的有效属性值。

我的错误是什么?

该应用程序运行良好,但我不喜欢 XAML 中的红色卷曲线。

【问题讨论】:

  • 您是否尝试过重新构建解决方案?它会消失的。
  • 是的,多次,没有帮助...
  • 它需要可以为空吗?你试过bool吗?
  • 您是否尝试过关闭并重新打开 XAML 文件?关闭并重新打开解决方案?在 VS 2013 之前,XAML 设计器似乎总是在缓存旧版本的程序集和/或错误消息时遇到问题。
  • 请在编辑中查看上述解决方案...

标签: c# wpf xaml dependency-properties


【解决方案1】:

如原海报所述:

找到了,原因是在两个Get/Set方法中找到的:

   /// <summary>
   /// attached property that defines if the source is a drag source
   /// </summary>
   public static readonly DependencyProperty IsDragSourceProperty =
      DependencyProperty.RegisterAttached("IsDragSource",
      typeof(bool?),
      typeof(DragDropBehaviour),
      new PropertyMetadata(null, new IsDragSourceBehaviour().PropertyChangedHandler));

   public static void SetIsDragSource(DependencyObject o, bool? propertyValue)
   {
       o.SetValue(IsDragSourceProperty, propertyValue);
   }

   public static bool? GetIsDragSource(DependencyObject o)
   {
       return (bool?)o.GetValue(IsDragSourceProperty);
   }

他们使用的是 object 而不是 bool?

【讨论】:

    猜你喜欢
    • 2011-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-29
    相关资源
    最近更新 更多