简体   繁体   English

无法将“ MultiBinding”类型的实例添加到“ DoubleCollection”类型的集合中

[英]Cannot add instance of type 'MultiBinding' to a collection of type 'DoubleCollection'

Error: I am trying to use a IMultiValueConverter I wrote, but Intellisense is giving me this error "Cannot add instance of type 'MultiBinding' to a collection of type 'DoubleCollection'. Only items of type 'double' are allowed." 错误:我试图使用我编写的IMultiValueConverter,但是Intellisense给我这个错误“无法将类型'MultiBinding'的实例添加到类型'DoubleCollection'的集合中。仅允许类型'double'的项目。”

Q: I do not understand what this error means. 问:我不明白此错误的含义。 I've used other converters to modify the StrokeDashArray properties in other paths; 我使用其他转换器来修改其他路径中的StrokeDashArray属性。 however, I am not sure how to use a multibinding. 但是,我不确定如何使用多重绑定。 Can someone explain why I am getting this error, and how I remove this error? 有人可以解释为什么我收到此错误,以及如何删除此错误吗?

Details: The converter is named "DashedWhenValue1ArrayEqualsValue2ArrayConverter" and I have defined it above as a StaticResource. 详细信息:该转换器名为“ DashedWhenValue1ArrayEqualsValue2ArrayConverter”,我在上面已将其定义为StaticResource。 Here are the interesting parts of my converter 这是我转换器的有趣部分

. . . 
public DoubleCollection DoubleCollectionWhenEqual { get; set; }
public DoubleCollection DoubleCollectionWhenNotEqual { get; set; }
public DoubleCollection DoubleCollectionWhenValueIsNull { get; set; }

. . .

public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{

  if (values == null || values.Length < 2)
  {
    return this.DoubleCollectionWhenValueIsNull;
  }

  if ( !(values[0] is Array) || !(values[1] is Array) )
  {
    return this.DoubleCollectionWhenNotEqual;
  }

  object[] array1 = values[0] as object[];
  object[] array2 = values[1] as object[];

  if (array1.Length != array2.Length)
  {
    return this.DoubleCollectionWhenNotEqual;
  }

  for (int i = 0; i < array1.Length; i++)
  {
    if (array1[i] != array2[i])
    {
      return this.DoubleCollectionWhenNotEqual;
    }
  }

  return this.DoubleCollectionWhenEqual;

}
. . .

I use my converter in the view here 我在这里的视图中使用转换器

<Path x:Name="some_Path" Data="M7,4.167 L7,162.08887" HorizontalAlignment="Right" Margin="0,0,-391.5,-871" StrokeStartLineCap="Square" StrokeEndLineCap="Square" Stroke="#FF33CC33" StrokeThickness="10" Width="10.5" RenderTransformOrigin="0,0" Height="167.167" VerticalAlignment="Bottom" >
      <Path.StrokeDashArray>
        <MultiBinding Converter="{StaticResource DashedWhenValue1ArrayEqualsValue2ArrayConverter}"> <!-- Error starts here -->
          <Binding Path="ModelViewProperty1" />
          <Binding Path="ModelViewProperty2" />
        </MultiBinding> <!-- Error ends here -->
      </Path.StrokeDashArray>
    </Path>

I was able to replicate your error in Visual Studio 2015. It does appear to be simply a bug with the XAML editor, not an actual problem with your XAML or converter. 我能够在Visual Studio 2015中复制您的错误。它似乎只是XAML编辑器的错误,而不是XAML或转换器的实际问题。 When I run the app, I get different dashing depending on whether ModelViewProperty1 and ModelViewProperty2 both have values and are equivalent. 当我运行该应用程序时,根据ModelViewProperty1ModelViewProperty2是否都具有值并且是否相等,我会得到不同的虚线。

So, my advice would be to ignore this particular error. 因此,我的建议是忽略此特定错误。

PS Just for fun, here is a more succinct way to write your Convert method using LINQ: PS只是为了好玩,这是使用LINQ编写Convert方法的更简洁的方法:

public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
    var arrays = values
        .OfType<Array>()
        .Select(x => x.OfType<object>())
        .ToList();
    if (arrays.Count != 2) return DoubleCollectionWhenValueIsNull;
    return arrays[0].SequenceEqual(arrays[1]) ? DoubleCollectionWhenEqual : DoubleCollectionWhenNotEqual;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法在Windows Phone 8.1 DatePicker中将类型&#39;%0&#39;的实例添加到类型&#39;%1&#39;的错误集合中 - Cannot add instance of type '%0' to a collection of type '%1' error in windows phone 8.1 DatePicker C#/ WPF .NET 4.0-无法将SetPropertyAction实例添加到TriggerActionCollection类型的集合中。 仅允许T项 - C#/WPF .NET 4.0 - Cannot add instance of SetPropertyAction to a collection of type TriggerActionCollection. Only items of T are allowed 为什么不能将此泛型实例添加到集合中? - Why can't I add this instance of a generic type to a collection? WPF MultiBinding Converter对象类型 - WPF MultiBinding Converter Object Type 检查实例是否是给定类型的集合 - Check if instance is a collection of given type 无法使用集合初始值设定项初始化类型 - cannot initialize type with a collection initializer 无法跟踪实体类型的实例 - The instance of entity type cannot be tracked 实体类型“”的实例无法跟踪 - The instance of entity type '' cannot be tracked 在类型上声明的方法不能用类型的实例调用 - Method declared on type cannot be called with instance of type 通用集合实例中的多类型约束 - Multiple type constraint in Generic Collection instance
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM