WPF .NET Core - Creating Objects With XAML |
Written by Mike James | ||||||||||
Thursday, 07 May 2020 | ||||||||||
Page 3 of 3
Reference Type PropertiesA second problem arises if we are trying to initialize a property that takes a reference type. This is mostly solved by the use of a different property initialization syntax. For example, instead of using an attribute to set MyProperty we use:
The result is the same but this syntax is more general because we can include a new object between the property tags. A demonstration of an object property is a little messy but if we add a new class:
This class has a single int property but in principle it could be as complicated as required. Now we can add a new class-valued property to our original class:
This is just a standard get/set property but it takes an object of type MyDataClass. Clearly this cannot be set to a new value using a string. The way to do it is:
This creates a new, nameless, instance of the DataClass class and then sets its MyProperty2 to 35. Notice that Attribute setting can be used for MyProperty2 because it is a supported value property however there would be no problem if MyProperty was another reference property – the nesting might get a little deep however! Implementing a type converterAs an alternative to using nested property syntax to set reference properties, we can opt to handle the conversion from the initializer string to the object properties ourselves. Surprisingly this isn’t difficult. For example, suppose we want to allow MyProperty2 to be set by a string. All we have to do is tag the type that the type converter will apply to with:
The rest of the class is unaltered. Now we have to implement the MyDataClassTypeConverter and to do this we need to add:
The type converter class has to inherit from TypeConverter and it first implements CanConvertFrom which simply returns true or false depending on whether or not the type converter can do the requested conversion:
The second method we need actually does the type conversion:
Notice that it creates a new instance of the class that it performs type conversion for and then processes the string to initialize the properties of the MyDataClass instance. Notice also that the Type converter creates and then initializes the object to be stored in the property using the information supplied by the initialization string. Clearly a real example would be more complex and would parse the string to extract multiple items of initialization information. With the type converter in place we can create an instance of MyClass using:
You will also discover that this approach puts both properties into the Property Windows so that they can be changed interactively. Beyond initialisationSuppose you want to make use of the objects created by the XAML in your code. The attribute:
defines the partial class that provides the “code behind” support for the XAML. Within this class all of the objects you have created are within the namespace and accessible. For example you can write, without any other modifications:
You can similarly write code that manipulates any object or property initialized by XAML. Where next?It is clear that XAML is a very general and extensible object instantiation language. If you follow the example in this article I can promise you lots of difficulties caused by synchronization – the project organization used in this article was adopted to make the example simple and thus falls over in the real world. The problem is that XAML doesn’t expect the assemblies that contain the classes it is instantiating to change during development. This is easily cured by splitting out the class definitions into another assembly. In the final analysis you have to ask yourself if XAML is worth it. Is it really so much better to instantiate objects declarative with all of the type conversion and namespace problems this brings about? For some situations the declarative approach to object creation makes sense - UIs for example. In other case you need to think carefully and if you decide to create a declarative system using XAML you will almost certainly have to create a custom editor for the objects. Knowing how XAML works, for WPF or UWP apps is well worth the effort however.
The Programmer's Guide To
|
WPF The Easy 3D Way WPF provides a full 3D graphics experience without the need to master DirectX. It really is the easy 3D approach. |
WPF .NET Core - Routed Events Routed events are a key feature of WPF. We look at bubbling and tunnelling and how to create your own routed event and how WPF changes the underlying mechanics of Windows. |
Custom BitmapSource Even if you don't anticipate implementing your own custom BitmapSource finding out how to reveals quite a lot about the way that WPF bitmaps work. |
Drawing Bitmaps – DrawingImage and DrawingVisual
WPF provides multiple ways to convert vector drawings to bitmaps. Find out how DrawingImage and DrawingVisual work and when to use which. On the way we look at how to create 2D vector drawings. |
The bitmap transform classes WPF bitmaps are immutable but that doesn't mean you can't transform them. Find out about rotation, scaling, changing pixel formatting and how to mangage color profiles. |
Other Articles |
Comments
or email your comment to: comments@i-programmer.info
<ASIN:0470041803>
<ASIN:1590599624>
<ASIN:0321374479>