我有以下XAML文本框:
<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>
我正在尝试将其绑定到转换器,代表以下内容:
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告诉我以下内容:
命名空间中不存在名称“ShippingLabelConverter” “CLR-名称空间:CIC.OrderProcessor”。
无效的标记扩展类型:期望的类型是 'IMultiValueConverter',实际类型是'ShippingLabelConverter'
我已经检查了转换器类的命名空间,这绝对是正确的。它还继承了'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"
答案 0 :(得分:1)
基本上你需要先创建一个转换器实例才能使用它:
<Window.Resources>
<local:ShippingLabelConverter x:Key="shippingLabelConverter" />
</Window.Resources>
和
<MultiBinding StringFormat="{}{0} {1}
{2}
{3}
{4}
{5}
{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
}
答案 1 :(得分:0)
我认为你需要定义一个静态资源。在您的XAML中:
<local:ShippingLabelConverter x:Key="shippingLabelConverter" />
您的文本框
<MultiBinding StringFormat="{}{0} {1}
{2}
{3}
{4}
{5}
{6} {7}" Converter="{StaticResource shippingLabelConverter}">