0

I created an WPF UserControl. For it's TextBox property binding I made a value converter class (implementing the IValueConverter interface).

Here is the C# code:

using System;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Globalization;

namespace appWPFtest
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }
    }

    public class StringToIntConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            string s = value.ToString();
            if (s.StartsWith("Не в сети")) return 0;
            else if (s == "Остановлено") return 1;
            else return 2;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new InvalidOperationException("NumberToBooleanConverter can only be used OneWay.");
        }
    }
}

I added this converter in the Control.Resources section. Here is the XAML code:

<Window x:Class="appWPFtest.Window1"
        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:appWPFtest"
        mc:Ignorable="d"
        Title="Window1" Height="450" Width="800">
    <Control.Resources>
        <local:StringToIntConverter x:Key="myConverter"/>
    </Control.Resources>
    <Grid>
    </Grid>
</Window>

Here is the question:

Now the converter is declared in the main namespace appWPFtest, can I somehow nest it inside the UserControl class, like this:

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

   public class StringToIntConverter : IValueConverter
   {
    ...
   }
}

When I do it and try to write in XAML:

<Control.Resources>
    <local:Window1.StringToIntConverter x:Key="myConverter"/>
</Control.Resources>

it becomes underlined in green...

And why when I type <local: this control doesn't appear in the IntelliSense? There are all objects of my namespace, except the current control...

P.S. I will be very grateful for pointing me to errors in my English ;)

Mustafa Özçetin
  • 1,893
  • 1
  • 14
  • 16

1 Answers1

0

If you try to hover the green underline in your xaml code, the displayed error couldn't be much clearer: Nested types are not supported

My question is: since your converter isn't directly involved in your class' logic, why you need to nest it?

It would be very simpler if you put it outside (either in the same file or in another) and use it like every other IValueConverter

Jesoo
  • 24
  • 2