You can always derive from DependecyObject class and add as many DependencyProperties as you want. For example:
ExampleConverter.cs
public class ExampleConverter : DependencyObject, IValueConverter
{
public string Example
{
get => GetValue(ExampleProperty).ToString();
set => SetValue(ExampleProperty, value);
}
public static readonly DependencyProperty ExampleProperty =
DependencyProperty.Register("Example", typeof(string), typeof(ExampleConverter), new PropertyMetadata(null));
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
//Do the convert
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
While the above answers may be feasible, they seem to be overly complicated. Simply use an IMultiValueConverter with an appropriate MultiBinding in the XAML code. Assuming that your ViewModel has the properties FirstValue, SecondValue, and ThirdValue, which are an int, a double, and a string, respectively, a valid multi converter might look like this:
C#
public class MyMultiValueConverter : IMultiValueConverter {
public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
int firstValue = (int)values[0];
double secondValue = (double)values[1];
string thirdValue = (string)values[2];
return "You said " + thirdValue + ", but it's rather " + firstValue * secondValue;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture) {
throw new NotImplementedException("Going back to what you had isn't supported.");
}
}
Since it requires neither fumbling with the ProvideValue method required by MarkupExtension, nor the specification of a DependencyObjectinside(!) a converter, I do believe that this is the most elegant solution.
Converters always implement IValueConverter. That means a call to Convert or ConvertBack passes a single additional parameter. That parameter is extracted from the XAML.
As Hitesh Patel suggests there is nothing to stop you putting more than one value into the parameter, so long as you have a delimiter to separate them out later, but you cannot use a comma as that delimits the XAML!
public object Convert(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
string parameterString = parameter as string;
if (!string.IsNullOrEmpty(parameterString))
{
string[] parameters = parameterString.Split(new char[]{'|'});
// Now do something with the parameters
}
}
Note, I have not checked it to see if a Pipe "|" character is valid in XAML there (should be), but if not just choose another character that does not clash.
Later versions of .Net do not require a character array for the simplest version of Split, so you can use this instead:
string[] parameters = parameterString.Split('|');
Addendum:
A trick eBay used to use in urls, years ago, was to delimit data in the URL with QQ. A double-Q does not naturally occur in text data. If you ever get stuck for a text delimiter that will avoid encoding issues just use QQ... This will not work with split though (which requires single characters, but nice to know) :)
You can always derive from DependecyObject class and add as many DependencyProperties as you want. For example:
ExampleConverter.cs
And then in XAML:
ExampleView.xaml
While the above answers may be feasible, they seem to be overly complicated. Simply use an
IMultiValueConverter
with an appropriateMultiBinding
in the XAML code. Assuming that your ViewModel has the propertiesFirstValue
,SecondValue
, andThirdValue
, which are anint
, adouble
, and astring
, respectively, a valid multi converter might look like this:C#
XAML
Since it requires neither fumbling with the
ProvideValue
method required byMarkupExtension
, nor the specification of aDependencyObject
inside(!) a converter, I do believe that this is the most elegant solution.This can be done using
System.Windows.Markup.MarkupExtension
(docs).This will allow you pass values to the converter which could be used as arguments or return values, for example:
Usage:
Be sure to reference the namespace of the converter in the
.xaml
.Converters always implement IValueConverter. That means a call to Convert or ConvertBack passes a single additional parameter. That parameter is extracted from the XAML.
As Hitesh Patel suggests there is nothing to stop you putting more than one value into the parameter, so long as you have a delimiter to separate them out later, but you cannot use a comma as that delimits the XAML!
e.g.
XAML
Converter
Note, I have not checked it to see if a Pipe "|" character is valid in XAML there (should be), but if not just choose another character that does not clash.
Later versions of .Net do not require a character array for the simplest version of
Split
, so you can use this instead:Addendum:
A trick eBay used to use in urls, years ago, was to delimit data in the URL with QQ. A double-Q does not naturally occur in text data. If you ever get stuck for a text delimiter that will avoid encoding issues just use QQ... This will not work with split though (which requires single characters, but nice to know) :)