System.Windows.Data.MultiBinding.Converter引发了异常

时间:2014-10-15 16:14:43

标签: c# xaml exception converter multibinding

尝试在我的xaml文档中实现一些代码。这里的想法是后面的代码将比较两个值,如果它们的字符串相等则返回True,但我不断收到System.Windows.Data.MultiBinding.Converter抛出异常

    <Grid xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:cvm="clr-namespace:Mashup;assembly=CompareValuesMashup"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase"
xmlns:dat="clr-namespace:System.Windows.Data;assembly=PresentationFramework"
xmlns:xlink="http://www.w3.org/1999/xlink">
        <Grid.Resources>
            <cvm:CompareTwoValues x:Key="CompareValues" />    
        </Grid.Resources>
        <Grid.ColumnDefinitions> 
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width="1*" />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="1*" />
        </Grid.RowDefinitions>
                <StackPanel Width="200" HorizontalAlignment="Left" Margin="5">
                    <!-- values to be processed by the converter -->
                    <TextBox Name="value1" Height="50" Margin="0,0,0,0" />
                    <TextBox Name="value2" Height="50" Margin="0,15,0,0" /> 
                    <!--  Compare value1 and value2-->
                    <TextBox Name="result1" Height="50" Margin="0,15,0,0">
                        <TextBox.Text>
                            <MultiBinding Converter="{StaticResource CompareValues}">
                                <Binding ElementName="value1" Path="Text" /> 
                                <Binding ElementName="value2" Path="Text" />
                            </MultiBinding> 
                        </TextBox.Text>
                    </TextBox>
                </StackPanel>
    </Grid>

这是背后的代码。我不确定异常的来源。

        namespace Mashup
{
  public class CompareTwoValues
  {
    public CompareTwoValues()
    {
      base..ctor();
    }

    public bool Execute(string value1, string value2)
    {
      return value1 == value2;
    }
  }
}

3 个答案:

答案 0 :(得分:0)

CompareTwoValues没有实施IMultiValueConverter。它没有实现任何接口或基类,所以我也不希望框架在类似场景中可以使用它。

它需要实现框架使用的接口:

public class CompareTwoValues : IMultiValueConverter
{
  public CompareTwoValues()
  {
    base..ctor();
  }

  public bool Execute(string value1, string value2)
  {
    return value1 == value2;
  }

  public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
  {
    // convert the values to the target type
  }

  public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
  {
    // convert the value back to the set of target types
  }
}

目前,类比较值,但它不会转换任何内容。它从现有的类逻辑中并不完全清楚转换的样子,但它需要发生才能在这种情况下使用。

将多个值转换为单个值的示例可能类似于:

return String.Concat(values[0], " ", values[1]);

转换回来可能是这样的:

return (value as string).Split(' ');

(例如,如果值是字符串,并且所需的结果是以空格分隔的连续字符串。)

答案 1 :(得分:0)

实现单个ValueConverter会不会更好? 像这样:

 public class CompareValues: IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return value == parameter;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

然后使用它

<!--  Compare value1 and value2-->
                <TextBox Name="result1" Height="50" Margin="0,15,0,0">
                    <TextBox.Text>
                        <Binding Converter="{StaticResource CompareValues}" ElementName="value1" Path="Text">
                            <Binding.ConverterParameter>
                                        <Binding Path="Text" ElementName="value2"/>
                                    </Binding.ConverterParameter>
                        </MultiBinding> 
                    </TextBox.Text>
                </TextBox>

答案 2 :(得分:0)

@david这有用或我的目的。我需要返回一个可以用来触发样式的字符串。

   public class IsEqual : IMultiValueConverter
{
    public object Convert(object[] values, Type targetTypes, object parameter, CultureInfo culture)
    {
        if (values[0] == "")
            return string.Format("blank");
        // returns yes if equal
        else if (string.Equals(values[0], values[1]))
            return string.Format("yes");
        //returns no if not equal
        else
            return String.Format("no");
    }
    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
    {
        return null;
    }
}