How to use Windows Community Toolkit

The Windows Community Toolkit is a collection of helper functions, custom controls, and app services. It simplifies and demonstrates common developer patterns when building experiences for Windows 10.

This tutorial will walk through adding and implementing the DataGrid control but the steps can be followed for any of the Uno ported Windows Community Toolkit controls.

Tip

The complete source code that goes along with this guide is available in the unoplatform/Uno.Samples GitHub repository - Uno Windows Community Toolkit Sample

Prerequisites


Tip

For a step-by-step guide to installing the prerequisites for your preferred IDE and environment, consult the Get Started guide.

Package ids for Uno Platform

Uno has ported the Windows Community Toolkit for use in Uno applications to allow for use on Windows, Android, iOS, macOS, and WebAssembly.

The following packages are available:

These package ids are for Uno (ie non-Windows) projects. For UWP and WinUI 3 projects, you should use the equivalent package ids published by Microsoft (Microsoft.Toolkit, Microsoft.Toolkit.Parsers etc).

Task 1 - Add Windows Community Toolkit to Uno Projects

  1. Install Nuget package for targeted control
    datagrid-nuget

Note: Be aware of which versions of Uno.UI are compatible with the Nuget package of each control.
If using version 3.0.0 or higher of Uno.UI, use version 6.1.0 or higher of DataGrid Nuget package.

Note: For UWP and WinUI 3 projects, you should use the packages published by Microsoft that are not prefixed with Uno.*.

  1. Add a reference to the UWP UI Controls

    In XAML:
    xmlns:controls="using:Microsoft.Toolkit.Uwp.UI.Controls"

    In C#:
    using Microsoft.Toolkit.Uwp;

  2. Implement the control.

Task 2 - Add the DataGrid Control

This control will create an easily organized grid that will allow you to create flexible columns and rows.

  1. Begin by adding the control using the syntax below. Change the x:Name to the name of your DataGrid.
    <controls:DataGrid x:Name="dataGrid"></controls:DataGrid>

  2. Add columns. Similar to how you would configure columns for a XAML Grid layout, you can add column definitions within your DataGrid control:

    <controls:DataGrid.Columns>
        <controls:DataGridTextColumn Header="Rank"/>
        <controls:DataGridComboBoxColumn Header="Mountain"/>
    </controls:DataGrid.Columns>
    

    This will create two columns that can be adjusted by the user. datagrid-column-gif

    Alternatively, you can use the AutoGenerateColumns attribute on your DataGrid control if you do not know how many columns your data will require.

    <controls:DataGrid x:Name="dataGrid" AutoGenerateColumns="True" />
    
  3. Format your rows in the same way as your columns or use a DataTemplate added as an attribute on the DataGrid control:

    <controls:DataGrid x:Name="dataGrid" RowDetailsTemplate="{StaticResource RowDetailsTemplate}">
    
  4. Data can be added with data binding. First, add your ItemsSource as a property of your DataGrid control.

    <controls:DataGrid x:Name="dataGrid" ItemsSource="{x:Bind MyViewModel.Customers}" />  
    

    Then, set the binding on each column:

    <controls:DataGrid.Columns>
        <controls:DataGridTextColumn Header="Rank" Binding="{Binding Rank}" Tag="Rank" />
        <controls:DataGridTextColumn Header="Mountain" Binding="{Binding Mountain}" Tag="Mountain" />
    </controls:DataGrid.Columns>
    

Referencing the Windows Community Toolkit from a Cross-Targeted Library

The Uno Platform build of the Windows Community toolkit is not needed when running on UWP, which is why you'll need to make some small changes to the project.

Adding the Uno version of the community toolkit to a Uno Platform cross-targeted library can cause build errors like this one:

Controls\TextBox\Themes\Generic.xaml : Xaml Internal Error error WMC9999: 
Type universe cannot resolve assembly: Uno.UI, Version=255.255.255.255, 
Culture=neutral, PublicKeyToken=null.

To fix this, instead of adding the Uno version of the toolkit like the code below:

<ItemGroup>
  <PackageReference Include="Uno.Microsoft.Toolkit.Uwp.UI.Controls" Version="7.0.0" />
</ItemGroup>

Add a conditional reference:

<ItemGroup Condition="'$(TargetFramework)' == 'uap10.0.17763'">
  <PackageReference Include="Microsoft.Toolkit.Uwp.UI.Controls" Version="7.0.0" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' != 'uap10.0.17763'">
  <PackageReference Include="Uno.Microsoft.Toolkit.Uwp.UI.Controls" Version="7.0.0" />
</ItemGroup>

You may need to replace uap10.0.17763 with the version defined in the TargetFrameworks node at the top of the csproj file.

See a working example with data

datagrid-full-sample

A working sample complete with data is available on GitHub: Uno Windows Community Toolkit Sample



Help! I'm having trouble

Tip

If you ran into difficulties with any part of this guide, you can: