Reputation: 9910
I have the following XAML textbox:
<TextBox x:Name="TextBoxShippingLabel" Margin="0,10,-2,2" TextWrapping="Wrap">
<TextBox.Text>
<MultiBinding StringFormat="{}{0} {1}
{2}
{3}
{4}
{5}
{6} {7}" Converter="{local:ShippingLabelConverter}">
<Binding ElementName="dataGridOutstandingOrders" Path="SelectedItem[FirstName]" />
<Binding ElementName="dataGridOutstandingOrders" Path="SelectedItem[Surname]" />
<Binding ElementName="dataGridOutstandingOrders" Path="SelectedItem[Department]" />
<Binding ElementName="dataGridOutstandingOrders" Path="SelectedItem[Organisation]" />
<Binding ElementName="dataGridOutstandingOrders" Path="SelectedItem[Street]" />
<Binding ElementName="dataGridOutstandingOrders" Path="SelectedItem[Suburb]" />
<Binding ElementName="dataGridOutstandingOrders" Path="SelectedItem[State]" />
<Binding ElementName="dataGridOutstandingOrders" Path="SelectedItem[Postcode]" />
</MultiBinding>
</TextBox.Text>
</TextBox>
I'm trying to bind this to a converter, that represents the following:
namespace CIC.OrderProcessor
{
public class ShippingLabelConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
var output = new StringBuilder();
foreach (var param in values.Cast<string>().Where(param => param != "None"))
{
output.Append(param);
}
return output.ToString();
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
However Visual Studio is telling me the following:
The name "ShippingLabelConverter" does not exist in the namespace "clr-namespace:CIC.OrderProcessor".
Invalid markup extension type: expected type is 'IMultiValueConverter', actual type is 'ShippingLabelConverter'
I've checked the namespace for my converter class and it's definitely correct. It's also inheriting the type 'IMultiValueConvterer' so I'm a bit unsure where to go from here - the errors seem like they should be obvious but I can't see any changes I should be making.
The declaration of the local namespace from my XAML is as follows:
<Window x:Class="CIC.OrderProcessor.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:CIC.OrderProcessor"
Upvotes: 0
Views: 604
Reputation: 2804
Essentially you need to create an instance of the converter before you can use it:
<Window.Resources>
<local:ShippingLabelConverter x:Key="shippingLabelConverter" />
</Window.Resources>
and
<MultiBinding StringFormat="{}{0} {1}
{2}
{3}
{4}
{5}
{6} {7}" Converter="{StaticResource shippingLabelConverter}">
Alternative: There is an alternative which is to implement another interface "MarkupExtension" so the converter knows how to provide an instance of itself. Check out this article: http://drwpf.com/blog/2009/03/17/tips-and-tricks-making-value-converters-more-accessible-in-markup/
public class DummyConverter : MarkupExtension, IValueConverter
{
private static DummyConverter _converter = null;
public override object ProvideValue(IServiceProvider serviceProvider)
{
if (_converter == null)
{
_converter = new DummyConverter();
}
return _converter;
}
#region IValueConverter Members
...
#endregion
}
Upvotes: 2
Reputation:
I think you need to define a static resource. In your XAML:
<local:ShippingLabelConverter x:Key="shippingLabelConverter" />
Your textbox
<MultiBinding StringFormat="{}{0} {1}
{2}
{3}
{4}
{5}
{6} {7}" Converter="{StaticResource shippingLabelConverter}">
Upvotes: 0