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

时间:2016-03-23 21:53:29

标签: c# wpf xaml converter

错误:我正在尝试使用我编写的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>

1 个答案:

答案 0 :(得分:2)

我能够在Visual Studio 2015中复制您的错误。它似乎只是XAML编辑器的一个错误,而不是您的XAML或转换器的实际问题。当我运行应用时,我会得到不同的潇洒,具体取决于import json for suffix in ['', '_ascii']: function_name = 'encode_basestring' + suffix orig_function = getattr(json.encoder, function_name) setattr(json.encoder, function_name, lambda s,_e=orig_function: _e(str(s))) ModelViewProperty1是否都具有值并且是等效的。

所以,我的建议是忽略这个特殊错误。

P.S。只是为了好玩,以下是使用LINQ编写ModelViewProperty2方法的更简洁方法:

Convert