简体   繁体   中英

Adding reference to a convertor defined inside a Class in XAML -Windows phone

Here is my modal

 public class items
        {
            public string item_name { get; set; }
            public string item_id { get; set; }
            public string item_quantity { get; set; }
            public string unit_id { get; set; }
        }
 public class services
        {
            public List<items> items { get; set; }
        }

I am assigning an object of the above class as itemsource for my List.I need to display unit name , but since my object contains only unit id, Im retrieving it from dictionary and using a convertor to bind it.

I am unable to reference this convertor from XAML.As this convertor is not directly inside namespace and within another class.

Namespace myname
{
     public partial class write : PhoneApplicationPage
     {
         public static Dictionary<String, String> units1 = new Dictionary<String, String>();

 public write()
         public class UnitConvertor : IValueConverter
         {
                public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
                {
                    string text = value.ToString();
                    if (text != null)
                    {
                        String unit = units1[text];
                        return unit;
                    }

                    return value;
                }

                public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
                {
                    return value;
                }
            }

      }
}

XAML PAGE:

xmlns:local="clr-namespace:myname"

I am not able to reference it under <Grid.resources>

Have your namespace as xmlns:local="clr-namespace:myname.write"

and you can access your Converter in the local namespace..

<Grid.resources>
    <local:UnitConvertor x:Key="unitConverter"/>
</Grid.resources>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM