Converters
Converters can help with creating dynamic elements across your theme. Listed are the available converters that can be used across your theme.
Written By Brian Faeran
Last updated 6 months ago
The following Namespace must be added to the UserControl at the top of any view to use any of these converters:
xmlns:converters="clr-namespace:Unbroken.LaunchBox.Windows.Converters;assembly=Unbroken.LaunchBox.Windows"AreEqualConverter
Returns true if the values are the same
Add to UserControl.Resources:
<converters:AreEqualConverter x:Key="AreEqualConverter"/>Example: When to set a trigger based on whether a game’s publisher and developer were the same
<DataTrigger Value="True">
<DataTrigger.Binding>
<MultiBinding Converter="{StaticResource AreEqualConverter}">
<Binding Path="SelectedGame.Publisher"/>
<Binding Path="SelectedGame.Developer"/>
</MultiBinding>
</DataTrigger.Binding>
<Setter Property="Visibility" Value="Collapsed"/>
</DataTrigger>AspectRatioStringConverter
Allows you to change content based on the aspect ratio of a user’s screen.
Values: Ultrawide Landscape, Ultrawide Portrait, Widescreen Landscape, Widescreen Portrait, Full-Frame Landscape, Full-Frame Portrait
Add to UserControl.Resources:
<converters:AspectRatioStringConverter x:Key="AspectRatio"/>Example: When the device's aspect ratio is in Full-Frame Landscape (approx.. 4:3), change Grid.Row number
<Image>
<Image.Style>
<Style TargetType="Image">
<Setter Property="Grid.Row" Value="0">
<Style.Triggers>
<DataTrigger Binding="{Binding Converter={StaticResource AspectRatio}}" Value="Full-Frame Landscape">
<Setter Property="Grid.Row" Value="2" />
</DataTrigger>
</Style.Triggers>
</Style>
</Image.Style>
</Image>BatteryLifeConverter
Returns the computer's battery life amount. The converter will return a full health value if the system isn’t battery powered, or we fail to get a life reading. The parameter of this converter is used to determine how many parts you want the life reported in.
Add to UserControl.Resources (Set ReturnWholeNumber=”False” if you want the value returned as a decimal):
<converters:AspectRatioStringConverter x:Key="BatteryLife" ReturnWholeNumber="True"/>Example: When trying to get the battery life to the nearest 4th (25%)
<TextBlock TextSize="{Binding Converter={StaticResource BatteryLife}, ConverterParameter=4}" />BindableThicknessConverter
Used in conjunction with our BindableThickness class to allow you to set part of a thickness (Used in properties such as Margin and Padding) via a binding, which is not possible in standard WPF
Add the BindableThickness to UserControl.Resources. This example shows all values being set with a static number, you can set any of these individually using any number or binding you wish. Any values NOT set will be set to 0:
<converters:BindableThickness x:Key="CustomThickness" Left="1" Top="2" Right="3" Bottom="4"/>Add to UserControl.Resources:
<converters:BindableThicknessConverter x:Key="BtConverter"/>Example: Setting margin thickness to a TextBlock
<TextBlock Margin="{Binding Source={StaticResource CustomThickness}, Converter={StaticResource BtConverter} />ContainsConverter
Returns true when the value contains the parameter. Works with enumerables, ItemControls, and strings.
Add to UserControl.Resources:
<converters:ContainsConverter x:Key="Contains"/>Example: Returns true when title contains “Mario”
<TextBlock TextBlock="{Binding Game.Title, Converter={StaticResource Contains}, ConverterParameter='Mario' />CustomFieldValueConverter
Fetches custom field values for a game.
Add to UserControl.Resources:
<converters:CustomFieldValueConverter x:Key="CustomFields"/>Example: When attempting to get the value of the field named “CustomField1”
<TextBlock Text="{Binding Game, Converter={StaticResource CustomFields}, ConverterParameter='CustomField1'}" />Example: When attempting to get the first value of the field named “CustomField1”
<TextBlock Text="{Binding Game, Converter={StaticResource CustomFields}, ConverterParameter='CustomField1[0]'}" />FindFileConverter
Will grab a file from a directory (if it exists). The converter has the following properties:
Directory (string)
What directory to look in
Extensions (optional string)
What extensions to accept. Multiple extensions should be separated by a semicolon (;). The converter will look for extensions in the order you type them and return the file with the first extension it finds
TopDirectoryOnly (optional bool)
Only look in the top most directory. When false will also look through subfolders for the file
Add to UserControl.Resources:
<converters:FindFileConverter x:Key="FindFile" Directory="LAUNCHBOX_THEME_FOLDER/Custom Images"/>Example: Find a file named the same as the game’s title
<TextBlock Text="{Binding Game.Title, Converter={StaticResource FindFile}}" />FindFileMultiConverter
Will grab a file from a directory (if it exists). This converter is different from the above in that you can pass multiple values as fallbacks and the converter will find the first available file. The converter has the following properties:
Directory (string)
What directory to look in
Extensions (optional string)
What extensions to accept. Multiple extensions should be separated by a semicolon (;). The converter will look for extensions in the order you type them and return the file with the first extension it finds
TopDirectoryOnly (optional bool)
Only look in the top most directory. When false will also look through subfolders for the file
Add to UserControl.Resources:
<converters:FindFileMultiConverter x:Key="FindFile" Directory="LAUNCHBOX_THEME_FOLDER/Custom Images"/>Example: Find a file named the same as the platform’s name. If it can’t find one, look for a file named after the platforms scrape as value
<TextBlock>
<TextBlock.Text>
<MultiBinding Converter="{StaticResource FindFile}">
<Binding Path="Platform.Name"/>
<Binding Path="Platform.ScrapeAs"/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>IsGreaterThanConverter
Will check if the value is greater than the parameter. Works with both numeric and string comparisons
Add to UserControl.Resources:
<converters:IsGreaterThanConverter x:Key="GreaterThan"/>Example: Return true if the game’s title comes after M
<TextBlock Text="{Binding Game.Title, Converter={StaticResource GreaterThan}, ConverterParameter='M'}" />IsGreaterThanOrEqualToConverter
Will check if the value is greater than the parameter. Works with both numeric and string comparisons. If requiring a “Less Than” comparison you can use this converter and when it returns false then the value is less than the parameter
Add to UserControl.Resources:
<converters:IsGreaterThanOrEqualToConverter x:Key="GreaterThanOrEqualTo"/>Example: Return true if the game’s title is or comes after M
<TextBlock Text="{Binding Game.Title, Converter={StaticResource GreaterThanOrEqualTo}, ConverterParameter='M'}" />IsLessThanOrEqualTo
Will check if the value is less than the parameter. Works with both numeric and string comparisons
Add to UserControl.Resources:
<converters:IsGreaterThanConverter x:Key="IsLessThanOrEqualTo"/>Example: Return true if the game’s title is (or comes before) M
<TextBlock Text="{Binding Game.Title, Converter={StaticResource IsLessThanOrEqualTo}, ConverterParameter='M'}" />IsNotEqualToConverter
Will check if the value is not equal to the parameter. Works with both numeric and string comparisons
Add to UserControl.Resources:
<converters:IsNotEqualTo x:Key="NotEqualTo"/>Example: Return true if the game’s title is not Mario
<TextBlock Text="{Binding Game.Title, Converter={StaticResource NotEqualTo}, ConverterParameter='Mario'" />IsNullOrEmptyConverter
Will return true if the value string is null or empty
Add to UserControl.Resources:
<converters:IsNullOrEmpty x:Key="NullOrEmpty"/>Example: Return true if the game’s doesn’t have a publisher value
<TextBlock Text="{Binding Game.Publisher, Converter={StaticResource NullOrEmpty}}" />IsNullOrWhitespaceConverter
Will return true if the value string is null, empty, or only whitespace characters
Add to UserControl.Resources:
<converters:IsNullOrWhitespace x:Key="NullOrWhitespace"/>Example: Return true if the game’s doesn’t have a publisher value
<TextBlock Text="{Binding Game.Publisher, Converter={StaticResource NullOrWhitespace}}" />ItemAtIndexConverter
This converter will return the item at the given index (zero based) in an enumerable, itemscontrol, or string. Strings will attempt to be split using the following characters (in order, stopping at the first one the string contains): semicolon (;), comma (,), forward slash (/), backslash (\)
Add to UserControl.Resources:
<converters:ItemAtIndexConverter x:Key="ItemAtIndex"/>Example: Returns the first genre value of the game
<TextBlock Text="{Binding Game.GenresString, Converter={StaticResource ItemAtIndex}, ConverterParameter=0}" />ItemOffsetConverter
This converter will return the item x number of positions away from the current selected item. This works with FlowControls and ItemsControls. The converter has a WrapAround property that can be set to determine if you want the offset to wrap around the edges of the collection. For example if set to true and you are asking for a -1 offset while the selected item is at index 0, you will get the last item in the collection.
Add to UserControl.Resources:
<converters:ItemOffsetConverter x:Key="ItemOffset" WrapAround="False"/>Example: Returns the game before the selected game from the FlowControl element named ‘FlowControl’
<TextBlock Text="{Binding ElementName=FlowControl, Converter={StaticResource ItemOffset}, ConverterParameter=-1}" />MultiplyByConverter
Will multiply a value by the parameter
Add to UserControl.Resources:
<converters:MultiplyBy x:Key="Multiply"/>Example: Multiply the font size binding by 1.25
<TextBlock Text="{Binding FontSize, Converter={StaticResource Multiply}, ConverterParameter=1.25}" />ScaleWithResolutionConverter
Allows you to maintain consistency sizing across various resolutions for properties like FontSize, Margin, Padding, BorderThickness, CornerRadius, etc.
Add to UserControl.Resources (based on a 2560x1440 resolution target):
<converters:ScaleWithResolutionConverter x:Key="ScaleConverter" SourceResolutionWidth="2560" SourceResolutionHeight="1440"/>Example: How to apply to FontSize and Margin
<TextBlock FontSize="{Binding Converter={StaticResource ScaleConverter}, ConverterParameter='32'}" Margin="{Binding Converter={StaticResource ScaleConverter}, ConverterParameter='10,0,0,0'}" />ToUpperConverter
Converts text to all upper case.
Add to UserControl.Resources:
<converters:ToUpperConverter x:Key="UpperCase"/>Example:
<TextBlock Text="{Binding Converter={StaticResource UpperCase}, Path=ActiveGame.Title}" />ToLowerConverter
Converts text to all lower case.
Add to UserControl.Resources:
<converters:ToLowerConverter x:Key="LowerCase"/>Example:
<TextBlock Text="{Binding Converter={StaticResource LowerCase}, Path=ActiveGame.Title}" />UpdateableDataTemplateConverter
The UpdateableDataTemplateConverter lets you define templates within a resource dictionary (such as UserControl.Resources) that can be applied anywhere templates are supported, created primarily for use within a transition presenter, with examples focused on that purpose.
Define the Converter
Inside <UserControl.Resources>:
<converters:UpdateableDataTemplateConverter x:Key="TransitionTemplate">
<converters:UpdateableDataTemplateConverter.Template>
<DataTemplate>
<Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<!-- Your content goes here -->
</Grid>
</DataTemplate>
</converters:UpdateableDataTemplateConverter.Template> </converters:UpdateableDataTemplateConverter> Use the Transition Presenter
In your main grid:
<transitions:TransitionPresenter Content="{Binding Converter={StaticResource TransitionTemplate}, Path=Game}">
<transitions:TransitionPresenter.Transition>
<transitions:FadeTransition Duration="0:0:1" />
</transitions:TransitionPresenter.Transition>
</transitions:TransitionPresenter> This links the converter template to the game binding and applies the chosen transition. Replace Game in the Path with the property that matches your current view context — for example: ActiveGame, SelectedGame, SelectedFilter, SelectedPlatform, etc…