trying to implement some code behind in my xaml document. The idea here is that the code behind will compare two values, and return True if they strings are equal, but i keep getting System.Windows.Data.MultiBinding.Converter threw an exception
<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>
here's the code behind. I'm not sure where the exception is coming from.
namespace Mashup
{
public class CompareTwoValues
{
public CompareTwoValues()
{
base..ctor();
}
public bool Execute(string value1, string value2)
{
return value1 == value2;
}
}
}
CompareTwoValues
doesn't implement IMultiValueConverter
. It doesn't implement any interface or base class, so I wouldn't expect it to be usable by the framework in similar scenarios either.
It needs to implement that interface to be used by the framework:
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
}
}
Currently the class compares values, but it doesn't convert anything. It's not entirely clear from the existing class logic what that conversion would look like, but it needs to happen in order to be used in this context.
An example of converting multiple values to a single value might be something like:
return String.Concat(values[0], " ", values[1]);
And converting back might be something like:
return (value as string).Split(' ');
(If, for example, the values were strings and the desired result is a concatenated space-delimited string.)