Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs(UnoCTK)Added examples for usage of converters second try from initial #18726 #19086

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
3b9648e
docs(uno-community-toolkit-v8): Added example(s) for usage of converters
DevTKSS Nov 7, 2024
171bf5c
Deleted trailing space at line 189
DevTKSS Jan 6, 2025
2b54d65
deleted azure listed intents and spacings
DevTKSS Jan 6, 2025
e08b3d5
chore(CTK): Deleted trailing space at line 189
DevTKSS Jan 6, 2025
24edaa7
chore(CTK): Deleted azure listed intents and spacings
DevTKSS Jan 6, 2025
f125f26
chore(CTK): Updated uno-community-toolkit-v8.md
DevTKSS Jan 6, 2025
f139bfe
Merge branch 'DevTKSSdocs(CTKConvertersUpdate)' of https://github.com…
DevTKSS Jan 7, 2025
89c381e
Merge branch 'unoplatform:master' into DevTKSSdocs(CTKConvertersUpdate)
DevTKSS Jan 7, 2025
f01ac40
Merge branch 'unoplatform:master' into DevTKSSdocs(CTKConvertersUpdate)
DevTKSS Jan 7, 2025
a92363f
docs(CTK): formatting and spellchecking on Converters pages
DevTKSS Jan 7, 2025
0265ec4
docs(CTK): commit for merge conflict fix applyed
DevTKSS Jan 7, 2025
8afb121
Merge branch 'unoplatform:master' into DevTKSSdocs(CTKConvertersUpdate)
DevTKSS Jan 9, 2025
6d2ab99
docs(CTK): formatting and spellchecking on Converters pages
DevTKSS Jan 7, 2025
367a965
chore(CTK): spellchecking and formatting
DevTKSS Jan 6, 2025
f074bc7
chore(CTK): Deleted azure listed intents and spacings
DevTKSS Jan 6, 2025
5bf7a80
refactor: Concrete versions of StartStopDelegateWrapper
MartinZikmund Sep 19, 2021
33d3a86
feat: Implement pattern/template grammar for DateTimeFormatter
Youssef1313 Jan 4, 2025
8d48614
Merge branch 'unoplatform:master' into DevTKSSdocs(CTKConvertersUpdate)
DevTKSS Jan 12, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 91 additions & 0 deletions doc/articles/uno-community-toolkit-v8.md
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,97 @@ You can set the `Header`, `HeaderIcon`, `Description`, and `Content` properties

A complete working sample, along with additional examples, is available on GitHub: [Uno Windows Community Toolkit SettingsCard Sample](https://github.com/unoplatform/Uno.Samples/tree/master/UI/WindowsCommunityToolkit/Version-8.x/UnoWCTSettingsCardSample)

## Using Non-UI Elements from the CommunityToolkit: Converters

The CommunityToolkit is providing some ready-to-use Converters for e.g. x:Bind in Xaml, whithout having to write already existing basic Converters yourself.
[List of CommunityToolkit Converters | Windows Toolkit Documentation](https://learn.microsoft.com/en-us/dotnet/communitytoolkit/windows/converters/)

The implementation of those are quite similar to the example of the SettingsControl above, but there are small adjustments to be done to use them:

1. Import of the Package

Change this:


```CommunityToolkit.WinUI.Controls.SettingsControls```

to Converters namespace:

```CommunityToolkit.WinUI.Converters```

while the Version will stay the same as above.

1. Add the related needed namespace(s)

> [!NOTE]
> In WCT version 8.x, the namespaces between UWP and WinAppSDK were merged.

### WinUI / WinAppSDK / UWP

In XAML:
```xmlns:converters="using:CommunityToolkit.WinUI.Converters"```

In C#:
```using CommunityToolkit.WinUI.Converters;```

In case you are developing a App that's using C# Markup and you want to use the Converters, you can now switch to [C#-Markup Converters](https://platform.uno/docs/articles/external/uno.extensions/doc/Learn/Markup/Converters.html) Documentation for future Usage Guide, the general Import is done from here on.

1. Xaml Definition

Important Difference to the previous seen SettingsCard Control Example, a Non-UI Converter has to be imported to the Page.Ressources Section to StaticRessources like this for using it, since there is no single Namespace per Converter like on the Controls:

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

StringToVisibilityConverter is a Converter that has to be bound to a String typed Property and will return a Visibility State.

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

Somewhere in your Page Content:

```xml
<TextBox x:Name="tbName"
Text="{Binding Name, Mode=TwoWay}"
PlaceholderText="Enter your name:"/>
<Button x:Name="StartButton"
Content="Start a cool simple Game!"
AutomationProperties.AutomationId="StartAGameButton"
Command="{Binding GoToStart}"
HorizontalAlignment="Center"
Visibility="{x:Bind tbName.Text, Converter={StaticResource StringVisibilityConverter}, Mode=OneWay}"/>
```

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

BoolToObjectConverter is a Converter that has to be bound to a Boolean typed Property and can return any Object you will give to it.
You only have to tell it what to return on True or False. If you would like to use it for switching color on validation:

```xml
BoolToObjectConverter x:Key="BoolToColorConverter" TrueValue="{ThemeResource SystemFillColorSuccessBackgroundBrush}"
FalseValue="{ThemeResource SystemFillColorCriticalBackgroundBrush}"/>
```

> [!NOTE]
> The used ThemeResource Brushes can be found in the WinUI Gallery for example.
> Feel free to use your own Colors e.g. from ColorPaletteOverrides

Somewhere in your Page Content:

```xml
<TextBox x:Name="tbName"
Text="{Binding Name, Mode=TwoWay}"
PlaceholderText="Enter your name:"
BackgroundColor="{x:Bind tbName.Text, Converter={StaticResource BoolToColorConverter},Mode=OneWay}/>
<Button x:Name="StartButton"
Content="Start a cool simple Game!"
AutomationProperties.AutomationId="StartAGameButton"
Command="{Binding GoToStart}"
HorizontalAlignment="Center"/>
```

---

[!include[getting-help](includes/getting-help.md)]
Loading
Loading