Skip to content

Commit 1dc598d

Browse files
authored
Update readme file.
1 parent e95b0b3 commit 1dc598d

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed

README.md

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,16 @@ public class MyViewModel : IBindingContext
350350

351351
> **Note:** You need to define `IntToStrConverter` to convert int to string. See the [PropertyValueConverter](#propertyvalueconvertertsourcetype-ttargettype) section for more information.
352352
353+
You can use the `Observable` attribute even on `public` properties to override the binding path.
354+
355+
```csharp
356+
public class MyViewModel : IBindingContext
357+
{
358+
[Observable("PreviousPropertyName")]
359+
public IReadOnlyProperty<string> NewPropertyName { get; }
360+
}
361+
```
362+
353363
#### Wrapping a non-observable model
354364

355365
A common scenario, for instance, when working with collection items, is to create a wrapping "bindable" item model that relays properties of the collection item model, and raises the property value changed notifications when needed.
@@ -762,6 +772,7 @@ The included UI elements are:
762772
- [BindableButton](#bindablebutton)
763773
- [BindableListView](#bindablelistview)
764774
- [BindableScrollView](#bindablescrollview)
775+
- [BindingContextProvider](#bindingcontextprovider)
765776

766777
> **Note:** The `BindableListView` & `BindableScrollView` are provided for `UI Toolkit` only.
767778
@@ -1020,6 +1031,73 @@ public class ImageViewerViewModel : IBindingContext
10201031
</UXML>
10211032
```
10221033

1034+
#### BindingContextProvider
1035+
1036+
The `BindingContextProvider` allows you to provide a custom `IBindingContext` for all child elements.
1037+
1038+
Let's say we have the following binding contexts.
1039+
1040+
```csharp
1041+
public class MainViewModel : IBindingContext
1042+
{
1043+
[Observable]
1044+
private readonly IReadOnlyProperty<string> _title;
1045+
1046+
[Observable]
1047+
private readonly IReadOnlyProperty<CustomViewModel> _customViewModel;
1048+
1049+
public MainViewModel()
1050+
{
1051+
_title = new ReadOnlyProperty<string>("Main Context");
1052+
_customViewModel = new ReadOnlyProperty<CustomViewModel>(new CustomViewModel());
1053+
}
1054+
}
1055+
```
1056+
1057+
```csharp
1058+
public class CustomViewModel : IBindingContext
1059+
{
1060+
[Observable]
1061+
private readonly IReadOnlyProperty<string> _title;
1062+
1063+
public CustomViewModel()
1064+
{
1065+
_title = new ReadOnlyProperty<string>("Custom Context");
1066+
}
1067+
}
1068+
```
1069+
1070+
To provide the `CustomViewModel` as a binding context for certain elements, we have to create a `BindingContextProvider` and use it as the parent for those elements.
1071+
1072+
Let's create a `CustomViewModelProvider` element.
1073+
1074+
```csharp
1075+
[UxmlElement]
1076+
public partial class CustomViewModelProvider : BindingContextProvider<CustomViewModel>
1077+
{
1078+
}
1079+
```
1080+
1081+
> **Note:** We use a [UxmlElement](#source-code-generator) attribute to create a custom control.
1082+
1083+
Now we can use the `CustomViewModelProvider` as follows.
1084+
1085+
```xml
1086+
<ui:UXML xmlns:uitk="UnityMvvmToolkit.UITK.BindableUIElements" ...>
1087+
<uitk:BindableLabel name="Label1" binding-text-path="Title" />
1088+
<!-- Binding context not specified. Will be used MainViewModel for all childs. -->
1089+
<CustomViewModelProvider>
1090+
<uitk:BindableLabel name="Label2" binding-text-path="Title" />
1091+
</CustomViewModelProvider>
1092+
<!-- Binding context is specified. Will be used CustomViewModel for all childs. -->
1093+
<CustomViewModelProvider binding-context-path="CustomViewModel">
1094+
<uitk:BindableLabel name="Label3" binding-text-path="Title" />
1095+
</CustomViewModelProvider>
1096+
</ui:UXML>
1097+
```
1098+
1099+
In this example, `Label1` and `Label2` will display the text "Main Context", while `Label3` will display the text "Custom Context".
1100+
10231101
### Source code generator
10241102

10251103
The best way to speed up the creation of custom `VisualElement` is to use source code generators. With this powerful tool, you can achieve the same great results with minimal boilerplate code and focus on what really matters: programming!

0 commit comments

Comments
 (0)