Keep getting error: The tag does not exist in XML namespace

孤人 提交于 2019-12-13 00:48:39

问题


I have a project that test Multibinding. I want binding data from textBox2 and textBox3 to textBox1. I tried again and again but still got error.

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>

MainWindow

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

And I got error:

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

So please tell me where's problem?


回答1:


There are a few of things wrong here. First, you don't need the converter. The StringFormat property of the MultiBinding will do this for you. Second, if you want to use a custom converter, you need to set the ConverterParameter on the MultiBinding, not StringFormat.

Now, the reason why your converter isn't in that namespace: you declared it inside your window class. The full name of your converter is Test_Multibiding.MainWindow.StringFormatConverter. It will compile (but your Converter will have a NullReferenceException as the parameter will be null) if you change your class to:

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



回答2:


You just need to move StringFormatConverter out of the MainWindow class, like so:

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



回答3:


StringFormatConverter is a sub-class of MainWindow and will not show in the root namespace of Test_Multibiding.

Move the declaration of StringFormatConverter outside of the scope of MainWindow but keep it in the Test_Multibiding namespace.



来源:https://stackoverflow.com/questions/18128062/keep-getting-error-the-tag-does-not-exist-in-xml-namespace

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!