不断获取错误:标记在XML命名空间中不存在



我有一个测试多绑定的项目。我想要将数据从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'.

所以请告诉我问题出在哪里?

这里有一些错误。首先,您不需要转换器。MultiBindingStringFormat属性将为您执行此操作。其次,如果要使用自定义转换器,则需要在MultiBinding上设置ConverterParameter,而不是StringFormat

现在,您的转换器不在该名称空间中的原因是:您在窗口类中声明了它。转换器的全名是Test_Multibiding.MainWindow.StringFormatConverter。如果您将类更改为:,它将编译(但您的Converter将有一个NullReferenceException,因为参数将为null)

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

您只需要将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");
    }
}

StringFormatConverterMainWindow的一个子类,不会显示在Test_Multibiding的根命名空间中。

StringFormatConverter的声明移到MainWindow的作用域之外,但将其保留在Test_Multibiding命名空间中。

相关内容

  • 没有找到相关文章

最新更新