Cheung
Cheung

Reputation: 15552

How to reference a sub class for IValueConverter?

I have read this SOF Post: How to properly reference a class from XAML But i cannot get this work. Because my converter class is subclass, i cannot get reference on XAML.

The Converter Class:

using System;
using System.Windows;
using System.Windows.Data;


namespace GapView.Classes
{
    public class ConverterClass
    {
        public class PhotoBorderConverter : IValueConverter
        {

            public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                int width = System.Convert.ToInt32(value);
                return width + 16;
            }

            public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                int width = System.Convert.ToInt32(value);
                return width - 16;
            }
        }
    }

}

And the MainWindow.xaml, XML section, i add

xmlns:GapView="clr-namespace:GapView"
xmlns:Classes="clr-namespace:GapView.Classes"

Inside

<Classes:ConverterClass x:Key="BorderConverter" /> 

Finally, i apply to Border element. And SettingThumbWidth is a TextBox element.

<Border  Width="{Binding Path=Text , ElementName=SettingThumbWidth, Converter={StaticResource BorderConverter}}" Height="166" >

When i press "." after BorderConverter, the subclass PhotoBorderConverter won't show and it seems i cannot access.

So how can i fix this?

It because it possible have other Converter, so i want to centralized in one ConvertClass.

Thanks you.

Upvotes: 1

Views: 1275

Answers (1)

slugster
slugster

Reputation: 49965

Your decision to centralize into ConverterClass is kind of weird and unnecesary. You can keep all your converters in one file, but you don't need to encapsulate them within an outer class.

With what you currently have, try using the correct namespace like this:

xmlns:converters="clr-namespace:GapView.Classes.ConverterClass"


<converters:PhotoBorderConverter x:Key="BorderConverter" />

Upvotes: 1

Related Questions