String Converter无法转换类型' MS.Internal.NamedObject'输入' System.String'

时间:2017-07-24 18:19:22

标签: c# exception

我创建了一个转换器,用于将单个字符串与逗号分隔的字符串进行比较,如果找到单个匹配则返回yes。我得到了一个例外

" System.InvalidCastException:无法转换类型' MS.Internal.NamedObject'的对象输入' System.String'。"

    public class IsEqual : IMultiValueConverter
{
    public object Convert(object[] values, Type targetTypes, object parameter, CultureInfo culture)
    {
        string value1 = (string)values[1];
        string[] splitVal = value1.Split(",".ToArray(), System.StringSplitOptions.RemoveEmptyEntries);

            foreach (var item in splitVal)
            {
                try
                {
                    string comparison = (string)values[0];

                    char[] delimiters = new char[1];
                    delimiters[0] = ',';

                    string[] split = ((string)values[1]).Split(delimiters);

                    foreach (string str in split)
                    {
                        if (comparison == str)
                        {
                            return String.Format("yes");
                        }
                    }
                }
                catch (Exception ex)
                {
                    return String.Format("Error: " + ex.ToString());
                }
                return String.Format("no");
            }
            return null;
        }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
    {
        return null;
    }

}

在标记内,它在DataGridColumn中用作DataTemplate,表示找到了匹配项:

<DataTemplate x:Key="Match">
<StackPanel>
    <Ellipse x:Name="Matches_Icon" Background="Green" HorizontalAlignment="Center" VerticalAlignment="Center" />
    <StackPanel VerticalAlignment="Center" HorizontalAlignment="Center" Visibility="Collapsed" Orientation="Vertical">
        <TextBox x:Name="value0" Text="{Binding ElementName=SingleItem,Path=Text,Mode=OneWay,TargetNullValue=0}" />
        <TextBlock x:Name="value1" Text="{Binding CommaDeliminatedItem,Mode=OneWay,TargetNullValue=0}" />
        <TextBox x:Name="Result" >
            <TextBox.Text>
                <MultiBinding Converter="{StaticResource IsEqual}">
                    <Binding ElementName="value0" Path="Text" Mode="OneWay" TargetNullValue="0" />
                    <Binding ElementName="value1" Path="Text" Mode="OneWay" TargetNullValue="0" />
                </MultiBinding>
            </TextBox.Text>
        </TextBox>
    </StackPanel>
</StackPanel>
<DataTemplate.Triggers>
    <DataTrigger Binding="{Binding ElementName=Result,Path=Text}" Value="yes">
        <Setter TargetName="SelectedArt_Icon" Property="Visibility" Value="Visible" />
    </DataTrigger>
    <DataTrigger Binding="{Binding ElementName=Result,Path=Text}" Value="no">
        <Setter TargetName="SelectedArt_Icon" Property="Visibility" Value="Collapsed" />
    </DataTrigger>
</DataTemplate.Triggers>

进一步深入研究,&#39; value1&#39;在XAML绑定中是DataGrid中的数据,value0是从数据网格外部提供的。找到匹配后,“是”&#39;返回(不知道为什么我们没有使用布尔..)并触发椭圆的可见性属性。

不应传入空值,因此我不知道此错误的来源。

从日志编辑: System.InvalidCastException:无法转换类型为&#39; MS.Internal.NamedObject&#39;的对象输入&#39; System.String&#39;。    at CustomMashupUI.IsEqual.Convert(Object [] values,Type targetTypes,Object parameter,CultureInfo culture)    在System.Windows.Data.MultiBindingExpression.TransferValue()    在System.Windows.Data.MultiBindingExpression.Transfer()    在System.Windows.Data.MultiBindingExpression.UpdateTarget(Boolean includeInnerBindings)    在System.Windows.Data.MultiBindingExpression.AttachToContext(Boolean lastChance)    在System.Windows.Data.MultiBindingExpression.MS.Internal.Data.IDataBindEngineClient.AttachToContext(Boolean lastChance)    在MS.Internal.Data.DataBindEngine.Task.Run(布尔值lastChance)    在MS.Internal.Data.DataBindEngine.Run(Object arg)    在System.Windows.Threading.ExceptionWrapper.InternalRealCall(委托回调,对象args,Int32 numArgs)    at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source,Delegate callback,Object args,Int32 numArgs,Delegate catchHandler)

1 个答案:

答案 0 :(得分:1)

问题是值是一个对象数组。如果要将成员转换为字符串类型,请使用:

string value1 = values[1].ToString();

string value1 = Convert.ToString(values[1]);