多值转换器不可访问

Multi-value converter not accesssable

我有以下 XAML 文本框:

<TextBox x:Name="TextBoxShippingLabel" Margin="0,10,-2,2" TextWrapping="Wrap">
    <TextBox.Text>
        <MultiBinding StringFormat="{}{0} {1}&#x0a;{2}&#x0a;{3}&#x0a;{4}&#x0a;{5}&#x0a;{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>

我正在尝试将其绑定到转换器,它表示以下内容:

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();
        }
    }
}

但是 Visual Studio 告诉我以下内容:

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'

我检查了我的转换器的名称空间 class,它绝对正确。它还继承了 'IMultiValueConvterer' 类型,所以我有点不确定从这里去哪里 - 错误看起来应该很明显,但我看不到我应该做的任何更改。

额外信息

我的XAML本地命名空间的声明如下:

<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"

我认为你需要定义一个静态资源。在你的 XAML:

<local:ShippingLabelConverter x:Key="shippingLabelConverter" />

你的文本框

        <MultiBinding StringFormat="{}{0} {1}&#x0a;{2}&#x0a;{3}&#x0a;{4}&#x0a;{5}&#x0a;{6} {7}" Converter="{StaticResource shippingLabelConverter}">

本质上,您需要先创建一个转换器实例,然后才能使用它:

<Window.Resources>
    <local:ShippingLabelConverter x:Key="shippingLabelConverter" />
</Window.Resources>

<MultiBinding StringFormat="{}{0} {1}&#x0a;{2}&#x0a;{3}&#x0a;{4}&#x0a;{5}&#x0a;{6} {7}" Converter="{StaticResource shippingLabelConverter}">

备选方案: 有一个备选方案是实现另一个接口 "MarkupExtension" 因此转换器知道如何提供其自身的实例。查看这篇文章: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
}