[英]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. 当我运行该应用程序时,根据
ModelViewProperty1
和ModelViewProperty2
是否都具有值并且是否相等,我会得到不同的虚线。
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.