Skip to content

Commit e95b0b3

Browse files
authored
Update readme file.
1 parent 488616f commit e95b0b3

File tree

1 file changed

+87
-27
lines changed

1 file changed

+87
-27
lines changed

README.md

Lines changed: 87 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -210,11 +210,7 @@ The included types are:
210210
211211
The `IBindingContext` is a base interface for ViewModels. It is a marker for Views that the class contains observable properties to bind to.
212212
213-
> **Note:** In case your ViewModel doesn't have a parameterless constructor, you need to override the `GetBindingContext` method in the View.
214-
215-
#### Simple property
216-
217-
Here's an example of how to implement notification support to a custom property.
213+
Here's an example of a simple ViewModel.
218214
219215
```csharp
220216
public class CounterViewModel : IBindingContext
@@ -228,25 +224,7 @@ public class CounterViewModel : IBindingContext
228224
}
229225
```
230226

231-
#### Wrapping a non-observable model
232-
233-
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.
234-
235-
```csharp
236-
public class ItemViewModel : IBindingContext
237-
{
238-
[Observable(nameof(Name))]
239-
private readonly IProperty<string> _name = new Property<string>();
240-
241-
public string Name
242-
{
243-
get => _name.Value;
244-
set => _name.Value = value;
245-
}
246-
}
247-
```
248-
249-
The `ItemViewModel` can be serialized and deserialized without any issues.
227+
> **Note:** In case your ViewModel doesn't have a parameterless constructor, you need to override the `GetBindingContext` method in the View.
250228
251229
### CanvasView\<TBindingContext\>
252230

@@ -273,6 +251,12 @@ public class CounterView : CanvasView<CounterViewModel>
273251
{
274252
return _appContext.Resolve<IValueConverter[]>();
275253
}
254+
255+
// Define a collection item templates.
256+
protected override IReadOnlyDictionary<Type, object> GetCollectionItemTemplates()
257+
{
258+
return _appContext.Resolve<IReadOnlyDictionary<Type, object>>();
259+
}
276260
}
277261
```
278262

@@ -301,6 +285,12 @@ public class CounterView : DocumentView<CounterViewModel>
301285
{
302286
return _appContext.Resolve<IValueConverter[]>();
303287
}
288+
289+
// Define a collection item templates.
290+
protected override IReadOnlyDictionary<Type, object> GetCollectionItemTemplates()
291+
{
292+
return _appContext.Resolve<IReadOnlyDictionary<Type, object>>();
293+
}
304294
}
305295
```
306296

@@ -312,9 +302,79 @@ Key functionality:
312302
- Provide a base implementation of the `IBaseProperty` interface
313303
- Implement the `IProperty<T>` & `IReadOnlyProperty<T>` interface, which exposes a `ValueChanged` event
314304

315-
The following shows how to set up a simple observable property:
316-
- [Simple property](#simple-property)
317-
- [Wrapping a non-observable model](#wrapping-a-non-observable-model)
305+
#### Simple property
306+
307+
Here's an example of how to implement a simple property.
308+
309+
```csharp
310+
public class CounterViewModel : IBindingContext
311+
{
312+
public CounterViewModel()
313+
{
314+
Count = new Property<int>();
315+
}
316+
317+
public IProperty<int> Count { get; }
318+
}
319+
```
320+
321+
```xml
322+
<ui:UXML xmlns:uitk="UnityMvvmToolkit.UITK.BindableUIElements" ...>
323+
<uitk:BindableLabel binding-text-path="Count" />
324+
</ui:UXML>
325+
```
326+
327+
> **Note:** You need to define `IntToStrConverter` to convert int to string. See the [PropertyValueConverter](#propertyvalueconvertertsourcetype-ttargettype) section for more information.
328+
329+
#### Observable property
330+
331+
```csharp
332+
public class MyViewModel : IBindingContext
333+
{
334+
[Observable("Count")]
335+
private readonly IProperty<int> _amount = new Property<int>();
336+
337+
// The field name will be used if you don't provide a property name.
338+
// Names '_title' and 'm_title' will be auto-converted to 'Title'.
339+
[Observable]
340+
private readonly IProperty<string> _title = new Property<string>();
341+
}
342+
```
343+
344+
```xml
345+
<ui:UXML xmlns:uitk="UnityMvvmToolkit.UITK.BindableUIElements" ...>
346+
<uitk:BindableLabel binding-text-path="Count" />
347+
<uitk:BindableLabel binding-text-path="Title" />
348+
</ui:UXML>
349+
```
350+
351+
> **Note:** You need to define `IntToStrConverter` to convert int to string. See the [PropertyValueConverter](#propertyvalueconvertertsourcetype-ttargettype) section for more information.
352+
353+
#### Wrapping a non-observable model
354+
355+
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.
356+
357+
```csharp
358+
public class ItemViewModel : IBindingContext
359+
{
360+
[Observable(nameof(Name))]
361+
private readonly IProperty<string> _name = new Property<string>();
362+
363+
public string Name
364+
{
365+
get => _name.Value;
366+
set => _name.Value = value;
367+
}
368+
}
369+
```
370+
371+
```xml
372+
<ui:UXML xmlns:uitk="UnityMvvmToolkit.UITK.BindableUIElements" ...>
373+
<uitk:BindableLabel binding-text-path="Name" />
374+
</ui:UXML>
375+
```
376+
377+
The `ItemViewModel` can be serialized and deserialized without any issues.
318378

319379
### Command & Command\<T\>
320380

0 commit comments

Comments
 (0)