Location>code7788 >text

Using ValueConverters extension to implement enumeration to control the display of the page

Popularity:910 ℃/2024-10-05 11:11:50

1、ValueConverters

This library contains theIValueConverterThe most common implementation of theValueConvertersUsed from the view to the view model is worth converting , in some cases , can be used for reverse conversion . Inside there are some abstract classes , template class definition , you can inherit these classes to achieve some of the functions you want to achieve , convenient and fast . LikeBoolToValueConverterBaseValueToBoolConverterBase, as well as template classes that inherit from these classes, we can all perform custom functions. The most important one is also aValueConverterGroupimplementation, you can put several Converters together to do the conversion one by one, and finally get the conversion from the initial value to the final value.

2. UseValueConverterGroupcarry out a conversion

This time we will use theValueConverterGroupThe realization of this from theEnumset a value ofVisibilityThe conversion of the interface content by the RadioButton can be realized.

2.1 New construction projects

The name of this project is:ValueConvertersApp, implemented using Prism framework + CommunityToolkit. Libraries referenced in this project include:

<PackageReference Include="" Version="8.3.2" />
<PackageReference Include="" Version="9.0.537" />
<PackageReference Include="ValueConverters" Version="3.1.22" />
2.2 EnumToBooleanConverter implementation

EnumToBooleanConverter inheritanceIValueConverterinterface, the main content of the implementation of the two methods, the specific implementation of the following:

public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
    var type = ();
    var parameterType = ();
    return  == 
        &&  == 
        && (parameter);
}


public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
    if (targetType == ())
    {
        return (targetType, ());
    }

    if (parameter is string str)
    {
        return (targetType, str);
    }
    return ;
}

In fact, there are implementations in the libraryEnumset a value ofVisibilityconversion, but in xaml you have to use a string to do so, this implementation above can be done just by enumerating class references. In case the enumeration string literal is written incorrectly.

2.3 Interface Implementation

Defines an enumeration type to control the display of the content of the MainWindow interface. enumeration class content:

public enum Page
{
    PageA,
    PageB,
    PageC,
    PageD,
}

The implementation of the content in the interface:

 <Grid>
     <>
         <RowDefinition Height="Auto" />
         <RowDefinition />
     </>
     <Border Margin="3">
         <StackPanel VerticalAlignment="Center" Orientation="Horizontal">
             <RadioButton Content="View A" IsChecked="{Binding Page, Converter={StaticResource EnumToBooleanConverter}, ConverterParameter={x:Static enums:}}">
             </RadioButton>
             <RadioButton
                 Margin="10,0"
                 d:IsChecked="true"
                 Content="View B"
                 IsChecked="{Binding Page, Converter={StaticResource EnumToBooleanConverter}, ConverterParameter={x:Static enums:}}">
             </RadioButton>
             <RadioButton Content="View C" IsChecked="{Binding Page, Converter={StaticResource EnumToBooleanConverter}, ConverterParameter={x:Static enums:}}">
             </RadioButton>
             <RadioButton
                 Margin="10,0,0,0"
                 Content="View D"
                 IsChecked="{Binding Page, Converter={StaticResource EnumToBooleanConverter}, ConverterParameter={x:Static enums:}}">
             </RadioButton>
         </StackPanel>
     </Border>
     <Border
         ="1"
         Margin="3"
         BorderBrush="OrangeRed"
         BorderThickness="2">
         <Grid>
             <Grid Visibility="{Binding Page, Converter={StaticResource EnumToVisibilityConverter}, ConverterParameter={x:Static enums:}}">
                 <TextBlock Text="View A">
                 </TextBlock>
             </Grid>
             <Grid Visibility="{Binding Page, Converter={StaticResource EnumToVisibilityConverter}, ConverterParameter={x:Static enums:}}">
                 <TextBlock Text="View B">
                 </TextBlock>
             </Grid>
             <Grid Visibility="{Binding Page, Converter={StaticResource EnumToVisibilityConverter}, ConverterParameter={x:Static enums:}}">
                 <TextBlock Text="View C">
                 </TextBlock>
             </Grid>
             <Grid Visibility="{Binding Page, Converter={StaticResource EnumToVisibilityConverter}, ConverterParameter={x:Static enums:}}">
                 <TextBlock Text="View D">
                 </TextBlock>
             </Grid>
         </Grid>
     </Border>
 </Grid>

Definition of the conversion class, in the middle:

xmlns:converters="clr-namespace:"
xmlns:convertervalue="clr-namespace:ValueConverters;assembly=ValueConverters"

 <converters:EnumToBooleanConverter x:Key="EnumToBooleanConverter" />
 <convertervalue:ValueConverterGroup x:Key="EnumToVisibilityConverter">
     <converters:EnumToBooleanConverter />
     <convertervalue:BoolToVisibilityConverter />
 </convertervalue:ValueConverterGroup>

3、Final effect

4. Summary

ValueConvertersThere are a lot of interesting implementations in it, so you can read the source code to see the exact functionality and improve your coding at the same time.