繁体   English   中英

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

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

错误:我试图使用我编写的IMultiValueConverter,但是Intellisense给我这个错误“无法将类型'MultiBinding'的实例添加到类型'DoubleCollection'的集合中。仅允许类型'double'的项目。”

问:我不明白此错误的含义。 我使用其他转换器来修改其他路径中的StrokeDashArray属性。 但是,我不确定如何使用多重绑定。 有人可以解释为什么我收到此错误,以及如何删除此错误吗?

详细信息:该转换器名为“ DashedWhenValue1ArrayEqualsValue2ArrayConverter”,我在上面已将其定义为StaticResource。 这是我转换器的有趣部分

. . . 
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;

}
. . .

我在这里的视图中使用转换器

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

我能够在Visual Studio 2015中复制您的错误。它似乎只是XAML编辑器的错误,而不是XAML或转换器的实际问题。 当我运行该应用程序时,根据ModelViewProperty1ModelViewProperty2是否都具有值并且是否相等,我会得到不同的虚线。

因此,我的建议是忽略此特定错误。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM