1、ValueConverters
This library contains theIValueConverter
The most common implementation of theValueConverters
Used 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 . LikeBoolToValueConverterBase
、ValueToBoolConverterBase
, as well as template classes that inherit from these classes, we can all perform custom functions. The most important one is also aValueConverterGroup
implementation, 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. UseValueConverterGroup
carry out a conversion
This time we will use theValueConverterGroup
The realization of this from theEnum
set a value ofVisibility
The 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 inheritanceIValueConverter
interface, 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 libraryEnum
set a value ofVisibility
conversion, 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
ValueConverters
There 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.