1

我有一个测试多重绑定的项目。我想将 textBox2 和 textBox3 中的数据绑定到 textBox1。我一次又一次地尝试,但仍然出现错误。

XAML:

<Window x:Class="Test_Multibiding.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="621"
    xmlns:c="clr-namespace:Test_Multibiding">
<Window.Resources>
    <c:StringFormatConverter x:Key="StrConverter"/>
</Window.Resources>
<Grid>
    <TextBox TextWrapping="Wrap" AcceptsReturn="True" Height="269" HorizontalAlignment="Left" Margin="376,22,0,0" Name="textBox1" VerticalAlignment="Top" Width="211" >
        <TextBox.Text>
            <MultiBinding Converter="{StaticResource StrConverter}" StringFormat="test {0} test {1} blabla">
                <Binding ElementName="textBox2" Path="Text"/>
                <Binding ElementName="textBox3" Path="Text"/>
            </MultiBinding>
        </TextBox.Text>
    </TextBox>
    <TextBox Height="40" HorizontalAlignment="Left" Name="textBox2" VerticalAlignment="Top" Width="222"/>
    <TextBox Height="40" HorizontalAlignment="Left" Name="textBox3" VerticalAlignment="Top" Width="222"/>

</Grid>

主窗口

namespace Test_Multibiding
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        public class StringFormatConverter : IMultiValueConverter
        {
            public object Convert(object[] values, Type targetType,
               object parameter, System.Globalization.CultureInfo culture)
            {
                return string.Format(parameter.ToString(), values);
            }

            public object[] ConvertBack(object value, Type[] targetTypes,
               object parameter, System.Globalization.CultureInfo culture)
            {
                throw new NotSupportedException("Cannot convert back");
            }
        }
    }
}

我得到了错误:

The tag 'StringFormatConverter' does not exist in XML namespace 'clr-namespace:Test_Multibiding'.

所以请告诉我哪里有问题?

4

3 回答 3

4

这里有一些问题。首先,您不需要转换器。的StringFormat财产MultiBinding将为您做到这一点。其次,如果要使用自定义转换器,则需要将 设置为ConverterParameteron MultiBinding,而不是StringFormat

现在,您的转换器不在该命名空间中的原因是:您在窗口类中声明了它。您的转换器的全名是Test_Multibiding.MainWindow.StringFormatConverter. 如果您ConverterNullReferenceException类更改为:

namespace Test_Multibiding
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    }

    public class StringFormatConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType,
           object parameter, System.Globalization.CultureInfo culture)
        {
            return string.Format(parameter.ToString(), values);
        }

        public object[] ConvertBack(object value, Type[] targetTypes,
           object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotSupportedException("Cannot convert back");
        }
    }
}
于 2013-08-08T14:07:18.780 回答
2

您只需要将StringFormatConverter移出MainWindow类,如下所示:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }
}

public class StringFormatConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType,
       object parameter, System.Globalization.CultureInfo culture)
    {
        return string.Format(parameter.ToString(), values);
    }

    public object[] ConvertBack(object value, Type[] targetTypes,
       object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotSupportedException("Cannot convert back");
    }
}
于 2013-08-08T14:06:51.817 回答
1

StringFormatConverter是 的子类,MainWindow不会显示在 的根命名空间中Test_Multibiding

将声明StringFormatConverter移到 MainWindow 范围之外,但将其保留在Test_Multibiding命名空间中。

于 2013-08-08T14:07:15.953 回答