Skip to content

Commit

Permalink
docs: Add aditional adjustments for WCT v8 converters documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
agneszitte committed Jan 20, 2025
1 parent e133692 commit d78d9bc
Showing 1 changed file with 43 additions and 28 deletions.
71 changes: 43 additions & 28 deletions doc/articles/uno-community-toolkit-v8.md
Original file line number Diff line number Diff line change
Expand Up @@ -195,13 +195,16 @@ The implementation of these is similar to the example of the `SettingsControl` a

### Single Project Template [WinUI / WinAppSDK]
1. Edit your project file `PROJECT_NAME.csproj` and add this additional needed reference:

```xml
<ItemGroup>
<PackageReference Include="CommunityToolkit.WinUI.Converters" />
<!-- Add more community toolkit references here -->
</ItemGroup>
```

1. Edit `Directory.Packages.props` and add the needed reference(s):

```xml
<ItemGroup>
<PackageVersion Include="CommunityToolkit.WinUI.Converters" Version="8.1.240916" />
Expand All @@ -226,46 +229,58 @@ The implementation of these is similar to the example of the `SettingsControl` a

1. XAML Definition

Unlike the previously seen `SettingsCard` control example, it is standard practice to define a converter in the `Page.Resources` section as a `StaticResource` before using it. This approach ensures that converters, like controls, are properly declared with a namespace and can be easily reused throughout the page.
Unlike the previously seen `SettingsCard` control example, it is standard practice to define a converter in the `Page.Resources` section as a `StaticResource` before using it. This approach ensures that converters, like controls, are properly declared with a namespace and can be easily reused throughout the page.

### [Example: StringToVisibilityConverter](#tab/string-visible-conv)
### [Example: StringToVisibilityConverter](#tab/string-visible-conv)

The `StringToVisibilityConverter` is a converter that transforms a string value into a `Visibility` state, returning `Visibility.Visible` for non-empty strings and `Visibility.Collapsed` for null or empty strings.
The `StringToVisibilityConverter` is a converter that transforms a string value into a `Visibility` state, returning `Visibility.Visible` for non-empty strings and `Visibility.Collapsed` for null or empty strings.

#### Define the Converter in Page Resources
#### Define the Converter in Page Resources

Add the converter to the `Page.Resources` section as a `StaticResource`:
Add the converter to the `Page.Resources` section as a `StaticResource`:

```xml
<Page.Resources>
<converters:StringToVisibilityConverter x:Key="StringToVisibilityConverter" />
</Page.Resources>
```
```xml
<Page.Resources>
<converters:StringToVisibilityConverter x:Key="StringToVisibilityConverter" />
</Page.Resources>
```

#### Use the Converter in Page Content
#### Use the Converter in Page Content

Here is an example of how to use the converter in your XAML content:
Here is an example of how to use the converter in your XAML content:

```xml
<TextBlock Text="This text is visible only if the condition is met."
Visibility="{Binding SomeStringProperty, Converter={StaticResource StringToVisibilityConverter}}"/>
```
```xml
<TextBlock Text="This text is visible only if the condition is met."
Visibility="{Binding SomeStringProperty, Converter={StaticResource StringToVisibilityConverter}}"/>
```

### [Example: BoolToObjectConverter](#tab/bool-obj-conv)
### [Example: BoolToObjectConverter](#tab/bool-obj-conv)

The `BoolToObjectConverter` allows you to convert a boolean value into a specific object by defining `TrueObject` and `FalseObject`. Depending on the boolean value, the converter will return the corresponding object.
The `BoolToObjectConverter` allows you to convert a boolean value into a specific object by defining `TrueObject` and `FalseObject`. Depending on the boolean value, the converter will return the corresponding object.

For example, you can use it to switch colors dynamically:
#### Define the Converter in Page Resources

```xml
<Page.Resources>
<converters:BoolToObjectConverter x:Key="BoolToColorConverter"
TrueObject="Green"
FalseObject="Red"/>
</Page.Resources>

<TextBlock Text="Status:"
Foreground="{Binding IsValid, Converter={StaticResource BoolToColorConverter}}"/>
For example, you can use it to switch colors dynamically.
Add the converter to the `Page.Resources` section as a `StaticResource`:

```xml
<Page.Resources>
<converters:BoolToObjectConverter x:Key="BoolToColorConverter"
TrueObject="Green"
FalseObject="Red"/>
</Page.Resources>
```

#### Use the Converter in Page Content

Here is an example of how to use the converter in your XAML content:

```xml
<TextBlock Text="Status:"
Foreground="{Binding IsValid, Converter={StaticResource BoolToColorConverter}}"/>
```

In this example, the `TextBlock` background will be green when `IsValid` is `true` and red when `IsValid` is `false`.

---

Expand Down

0 comments on commit d78d9bc

Please sign in to comment.