You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
XAML namespace was a feature inherited from xml, it allows to import namespaces at any level of element, and all of its child elements within the scope can access the namespace.
@@ -64,14 +43,22 @@ Each child element within the scope can access it.
64
43
- `x:DataType`: the `DataContext`(aka ViewModel in MVVM) for current control.
65
44
- `x:Name`: name of the instance of control within the code-behind class.
66
45
- XAML compiler would generate an instance by that name for your code-behind class.
67
-
- avalonia has a `StyledElement.Name` property available, which is an equivalence of `x:Name`
68
-
- `x:Static`: accessor for **static members in assembly**
46
+
- avalonia has a `StyledElement.NameProperty` available, which is an equivalence of `x:Name`
47
+
- `x:Type`: accessor for types, can be used as a **markup extension**. It's the equivalent of using the `typeof` operator.
48
+
```xml
49
+
<Window.DataTemplates>
50
+
<DataTemplateDataType="{x:Type local:Student}">
51
+
<!-- ... -->
52
+
</DataTemplate>
53
+
</Window.DataTemplates>
54
+
```
55
+
-`x:Static`: accessor for **static members in assembly**, can be used as a **markup extension**.
One should know about avalonia types before learning XAML, each tag in XAML constructs a instance from corresponding type.
4
-
XAML also has some implicit bindings for special types with certain properties, such as `ContentControl.Content` and `ItemsControl.Items`.
5
-
There's two critical base classes in avalonia:
6
-
-`AvaloniaObject`: a type containing a bunch of `AvaloniaProperty`
7
-
- all components/controls are of `AvaloniaObject`, such as `Window`, `Button`.
8
-
-`AvaloniaProperty`: how avalonia defined a **shared property metadata** for property **owners**
9
-
-`StyledProperty`: property definition supports styling overriding and inheritance
10
-
-`DirectProperty`: simple property for storing plain data
11
-
12
3
## AvaloniaObject
13
4
14
5
All controls are derived from `AvaloniaObject` class.
15
6
There's some special and critical control base class you may encounter when using avalonia.
16
7
-`StyledElement`: a minimal useable control base which starts styling support, most of controls became applicable after this class
17
-
-`ContentControl`: control for storing singular content
18
-
-`ItemsControl`: control for storing multiple items
8
+
-`ContentControl`: control for presenting singular content
9
+
-`ItemsControl`: control for presenting multiple items
19
10
-`UserControl`: TODO
20
11
-`TemplatedControl`: TODO
21
12
@@ -91,8 +82,8 @@ public class AvaloniaObject : IAvaloniaObjectDebug, INotifyPropertyChanged {
91
82
### Direct Property
92
83
93
84
`DirectProperty` is a minimal property type registered for **plain data**, ideal for **indicators** of a control, such as `Button.IsPressed` or a **statistic** such as `ItemsControl.ItemCount` or even an **collection**.
94
-
Unlike `StyledProperty` which stores backing values in `ValueStore`, `DirectProperty` simply requires a true**backing field** within the class.
95
-
`SetAndRaise` will raise `AvaloniaObject.PropertyChanged` event **when the new value is different than old one.**
85
+
Unlike `StyledProperty` which stores backing values in `ValueStore`, `DirectProperty` simply requires a dedicated**backing field** within the class.
86
+
`AvaloniaObject.SetAndRaise` will raise `AvaloniaObject.PropertyChanged` event **when the new value is different than the old.**
> It doesn't mean that a plain data can only be of `DirectProperty`, it can be a `StyledProperty` depending on its role, `Layoutable.HeightProperty` for example is a `StyledProperty`.
102
+
109
103
### Attached Property
110
104
111
-
TODO: what is that?
105
+
`AttachedProperty` is a static property presentation that, it **does not require instance member as backing source**(like what `StyledProperty` and `DirectProperty` required) but two **conventional static getter/setter** methods to be invoked on runtime.
106
+
107
+
Besides `TOwner` and `TValue`, `AttachedProperty` has an extra type parameter
108
+
109
+
-`THost`: which kind of type the property can attach to
110
+
111
+
Static `Design` class is one of the great examples of attached property, what you have bind for `Design.DataContext` is exactly a property attached to your current control(`MainWindow` for example)
112
+
You would notice that `AttachedProperty` would require two **static** methods with conventional name, those methods would be invoked by runtime by their special name.
113
+
And the property value would be **still managed by `AvaloniaObject.GetValue` and `AvaloniaObject.SetValue`**, meaning that it has **same mechanism** as `StyledProperty`(the `ValueStore` was involved).
> Yes, XAML compiler would recognize the attached property and properly set the value at runtime.
152
+
> Even though it was written in an instance construction syntax.
153
+
112
154
113
155
### Reuse Property Definition
114
156
@@ -161,11 +203,50 @@ public IBinding this[IndexerDescriptor binding] {
161
203
}
162
204
```
163
205
164
-
## Data Context
206
+
## StyledElement.DataContext
165
207
166
208
`StyledElement.DataContext` is **the instance of view model** which allows you to access(or even mutate) the state of view model within a control(code-behind).
167
209
Avalonia **searches upward hierarchically** for `DataContext` property until top level element, meaning that controls can share context with children.
168
210
211
+
::: details definition
212
+
213
+
```cs
214
+
publicclassStyledElement : Animatable,
215
+
IDataContextProvider,
216
+
ILogical,
217
+
IThemeVariantHost,
218
+
IResourceHost2,
219
+
IStyleHost,
220
+
ISetLogicalParent,
221
+
ISetInheritanceParent,
222
+
ISupportInitialize,
223
+
INamed,
224
+
IAvaloniaListItemValidator<ILogical>,
225
+
#pragmawarningdisable CS0618 // Type or member is obsolete
226
+
IStyleable
227
+
#pragmawarningrestore CS0618 // Type or member is obsolete
@@ -187,8 +268,7 @@ public partial class MainWindow : Window {
187
268
188
269
Views beside `MainWindow`(which is the default window generated by avalonia project Template) needs explicit set for `DataContext`, because `MainWindow.DataContext` was assigned in `App.axaml.cs` so you don't need to worry about it.
- `ControlTemplate`: common template type for `TemplatedControl.TemplateProperty`
344
+
345
+
- Template Properties:
346
+
-`Control.DataTemplates`: a direct collection of `IDataTemplate`(not a `AvaloniaProperty`)
347
+
- provides a way to **present any type(such as a model) as a control**
348
+
-`TemplatedControl.TemplateProperty`: how a control was presented, you may override this property for controls to alter its default appearance
349
+
-`ContentControl.ContentTemplateProperty`: how `Content` was presented
350
+
-`ItemsControl.ItemTemplateProperty`:
351
+
352
+
353
+
### IDataTemplate
354
+
355
+
The common builtin presenter for displaying objects other than `AvaloniaObject` with certain format, implements `IDataTemplate`.
356
+
A typical usage of `IDataTemplate` is `ViewLocator` which determines what kind of control can
357
+
**Yes, controls are not specially treated by XAML compiler,** they were only allowed when you specify `.DataTemplates` for `Application` so that the application could load controls by the context object at runtime(**using reflection**).
358
+
359
+
> [!NOTE]
360
+
> If one were to compile ahead-of-time which could not use reflection, should use `CompiledBinding` markup and `DataTemplate.DataType` to inform XAML compiler explicitly.
`ContentControl.ContentTemplate` is of type `IDataTemplate` that is commonly overridden by `DataTemplate`, the default **presentation of custom data formatting** type.
421
+
The whole point of `DataTemplate` is to provide a way to format `ContentProperty` using given format.
422
+
423
+
<!-- TODO: add example -->
424
+
425
+
### ItemsControl.ItemTemplate
426
+
427
+
Similar to `ContentControl.ContentTemplate` but for presenting items
428
+
429
+
## ControlTemplate
430
+
431
+
A common placeholder type to present how to re-structure a control's template looking(which you don't have access to modify the control's source definition).
432
+
This is typically used to override `TemplatedControl.Template` and with `TemplateBinding` markup extension to read context from containing control.
Presenter controls are control scaffold for representing certain content as a **wrapper** itself, so that it can bind **any source** of content from context within it.
445
+
446
+
### ContentPresenter
447
+
448
+
`ContentPresenter` is a type of placeholder for singular content. It's a lightweight element whose sole purpose is to **define a binding declaration** for displaying the content of a `ContentControl`
449
+
So it's typically seen in declaration of `TemplatedControl.Template` and using with `TemplateBinding` markup extension to read context from containing control.
0 commit comments