0

I was looking around for how to do a placeholder in WPF, and I found the answer here. I used the XAML Code in my file, and it gave me the following error: The Type local:TextInputToVisibilityConverter Could Not Be Found. The line looks like this:

<local:TextInputToVisibilityConverter x:Key="TextInputToVisibilityConverter" />

I'm confused why it's giving me this error because I have the TextInputToVisibilityConverter in my c# code:

    public class TextInputToVisibilityConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            // Always test MultiValueConverter inputs for non-null
            // (to avoid crash bugs for views in the designer)
            if (values[0] is bool && values[1] is bool)
            {
                bool hasText = !(bool)values[0];
                bool hasFocus = (bool)values[1];

                if (hasFocus || hasText)
                    return Visibility.Collapsed;
            }

            return Visibility.Visible;
        }


        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

Help would be greatly appreciated.

Community
  • 1
  • 1
  • I like how you explain that you have an error in your XAML, but then you are going to not really show any of your XAML document... :) Have you tried to build you project despite this error? (the XAML designer sometimes fails to identify/know custom types if they have not been compiled/built yet). Have you declared the "local" namespace correctly? –  May 14 '17 at 16:28
  • @elgonzo I did. It is `` – Nathan Chan May 14 '17 at 16:56
  • So, this line should tell somehow how you declared the "local" namespace? Is the TextInputToVisibilityConverter class in the same C# namespace as declared by the "local" namespace in the XAML? –  May 14 '17 at 17:01
  • @elgonzo I declared the local namespace like this: `` – Nathan Chan May 14 '17 at 17:03
  • Okay. In your C# file for the TextInputToVisibilityConverter, is the same namespace (Compari_Maker_2017) being used? –  May 14 '17 at 17:04
  • @elgonzo Compari_Maker2017 is the name of my program if you were wondering what's in my `xmlns:local` – Nathan Chan May 14 '17 at 17:04
  • 1
    Na, the particular name of the namespace itself is not relevant. It is only relevant that the TextInputToVisibilityConverter class is in the **same** namespace. –  May 14 '17 at 17:05
  • @elgonzo my c# file is above – Nathan Chan May 14 '17 at 17:05
  • You mean, your C# file for TextInputToVisibilityConverter does not declare any namespace? –  May 14 '17 at 17:07
  • @elgonzo I declared my namespace like this `namespace Compari_Maker_2017` – Nathan Chan May 14 '17 at 17:07
  • 1
    That looks okay. Have you tried to build your project despite the error message in the designer? –  May 14 '17 at 17:08
  • Yes, and it still gave me the error. I'm using Visual Studio 2017 if that helps – Nathan Chan May 14 '17 at 17:15
  • Hmm... strange. I haven't much experience with VS2017 (being still stuck with VS2015), so i can't tell whether this is some glitch in the VS2017 XAML designer, or whether there is something else wrong with your class declaration of TextInputToVisibilityConverter. Just to make sure, in the C# file of TextInputToVisibilityConverter, you don't use nested namespaces, right? –  May 14 '17 at 17:16
  • @elgonzo I don't have a namespace inside a namespace, but I have a class in a class – Nathan Chan May 14 '17 at 17:26
  • What exactly do you mean? Is TextInputToVisibilityConverter inside another class? –  May 14 '17 at 17:30
  • @elgonzo `TextInputToVisibilityConverter` is inside my window class – Nathan Chan May 14 '17 at 17:37
  • Erm... that cannot work :) The actual full class name of the inner class without namespace is `MyWindow.TextInputToVisibilityConverter` (or whatever the name of your window class); not just "TextInputToVisibilityConverter". Unfortunately you cannot use names of nested classes in XAML like that (aside from a few exceptions). Move the converter class out of your window class , i.e., do not make it an inner/nested class... –  May 14 '17 at 17:38
  • @elgonzo Now it says that `TextInputToVisibilityConverter` doesn't exist in the `Compari_Maker_2017` – Nathan Chan May 14 '17 at 19:07
  • I did say to move the converter outside of your window class. I did not say to move it outside of the namespace ;) –  May 14 '17 at 19:07
  • @elgonzo It's in my namespace: [image here](https://image.ibb.co/hDT5MQ/Screenshot_1.png) – Nathan Chan May 14 '17 at 19:27
  • Where exactly does it say the error? Have you tried building your project again? –  May 14 '17 at 19:43
  • @elgonzo It still doesn't work – Nathan Chan May 14 '17 at 19:58
  • Still the same error? When you run the program, does the converter actually work? What i mean: Is the error message limited to the XAML designer, or is your actual running program also affected (when you build and run the program now)? –  May 14 '17 at 20:01
  • @elgonzo The whole program is effected – Nathan Chan May 14 '17 at 20:19
  • There is something else wrong i can't see from here, as i have not the complete picture of your code project. From your screenshot, TextInputToVisibilityConverter appears to be in the Compari_Maker_2017 namespace. It does not seem to be in a separate assembly (otherwise it would not be in the same source project together with NewWindow). I have no idea why the error says that it cannot be found in that namespace. (Since your running program is also affected, i believe a glitch in the XAML designer can be ruled out...) –  May 14 '17 at 20:29

1 Answers1

0
public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            // Always test MultiValueConverter inputs for non-null
            // (to avoid crash bugs for views in the designer)
            if (values[0] is bool && values[1] is bool)
            {
                bool hasText = !(bool)values[0];
                bool hasFocus = (bool)values[1];

                if (hasFocus || hasText)
                    return Visibility.Collapsed;
            }

            return Visibility.Visible;
        }


        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
        {
            string[] values = null;
            if (value != null)
            {
                values = value.ToString().Split(' ');
                return values;
            }
            else
            {
                return null;
            }
        }

You should have changed the same name converter x:key:name and should have filled the Convert back.

ouflak
  • 2,458
  • 10
  • 44
  • 49