Consider the following code:
[Serializable]
public class Region
{
public double North {get; set;}
public double South {get; set;}
public double East {get; set;}
public double West {get; set;}
public static Region Europe { get { return new BoundingBox() { North= 71, South= 36, East= 42, West= -9 }; } }
public static Region GulfMexico { get { return new BoundingBox() { North = 30, South = 18, East = -97, West = -80 }; } }
public static Region Australia { get { return new BoundingBox() { North = -11, South = -40, East = 154, West = 114 }; } }
public static Region ...
}
public class MyClass
{
public Region SelectedRegion { get; set; }
}
I would like to create an instance of MyClass in XAML and set its SelectedRegion property both of the following ways just like we can do with Brush-es when setting Grid.Background:
First:
<MyClass>
<MyClass.SelectedRegion>
<Region North ="5" South = "6" East = "7" West = "8"/>
</MyClass.SelectedRegion>
</MyClass>
Second:
<MyClass SelectedRegion = "Australia"/>
The first way of setting SelectedRegion works, of course. What is it I need to add to my code to facilitate the second way?
You need to create a
TypeConverterthat will convert a string (“Australia”) to an instance of theRegiontype. See the following link for details on how to do it: http://msdn.microsoft.com/en-us/library/aa970913.aspx.