CommunityToolkit / dotnet

.NET Community Toolkit is a collection of helpers and APIs that work for all .NET developers and are agnostic of any specific UI platform. The toolkit is maintained and published by Microsoft, and part of the .NET Foundation.
https://docs.microsoft.com/dotnet/communitytoolkit/?WT.mc_id=dotnet-0000-bramin
Other
2.99k stars 294 forks source link

Add `IValueConverter`s to CommunityToolkit.MVVM #670

Closed AchimStuy closed 1 year ago

AchimStuy commented 1 year ago

Overview

We would like to migrate from our own MVVM library to CommunityToolkit.MVVM. In our library we have implemented several frequently used IValueConverters, which I would like to have in the CommunityToolkit. Can these be added? Where? I'd propose a new folder Converters in the CommunityToolkit.Mvvm project.

API breakdown

(each with Reverse property)

Usage example

<Window
    xmlns:conv="clr-namespace:CommunityToolkit.Mvvm.Converters;assembly=CommunityToolkit.Mvvm"
    >
    <Window.Resources>
        <conv:NullBooleanConverter x:Name="NullBooleanConverter" />
        <conv:NullVisibilityConverter x:Name="NullVisibilityConverter" />
    </Window.Resources>

    <Grid>
        <ListBox ItemsSource="{Binding Items}" Visibility="{Binding Items, Converter={StaticResource NullVisibilityConverter}}" />
        <Button Content="Submit" IsEnabled="{Binding Data, Converter={StaticResource NullBooleanConverter}}" />
    </Grid>
</Window>

Breaking change?

No

Alternatives

The converters could also be added to the .NET code, but this would not make it available to our net48 applications.

Additional context

No response

Help us help you

Yes, I'd like to be assigned to work on this item

cremor commented 1 year ago

The IValueConverter interface is platform specific (it's a different type for WinForms, WPF, UWP, MAUI, ...). So it's not possible to provide value converters with the platform independent target frameworks that the MVVM Toolkit currently uses.

The "Windows Community Toolkit" and "MAUI Community Toolkit" projects already provide converters: https://learn.microsoft.com/en-us/windows/communitytoolkit/helpers/converters https://learn.microsoft.com/en-us/dotnet/communitytoolkit/maui/converters/

It seems like there is no "WPF Community Toolkit" project yet. That would have to provide converters for WPF (which I assume you meant, because you linked the WPF IValueConverter interface).

Sergio0694 commented 1 year ago

Yeah all converters are in framework specific libraries, such as the Windows Community Toolkit 🙂 We don't really plan on adding them to the MVVM Toolkit, which is platform agnostic. Additionally, converters should be avoided when possible on new frameworks (using x:Bind to function instead).

AchimStuy commented 1 year ago

Oh, I thought the System.Windows.Data.IValueConverter would be used for all Frameworks. Didn't know that.

If you should ever decide to make a WPF Community Toolkit, just tell me and I'll provide the converters and some other stuff. 🙂