In .NET 8 MAUI, I have my custom component that returns an object
public class LanguageModel
{
public string? LanguageName { get; set; }
public string? Abbreviation { get; set; }
public string? Flag { get; set; }
public bool IsSupported { get; set; } = false;
}
Now, I want to add my component in a ContentView
and add a Trigger
or a DataTrigger
to change, for example, the colour of the text. I tried also with a behaviour but it is not working.
For that, I created a simple Converter
like this:
public class IsLanguageEmpty : IValueConverter
{
public object? Convert(object? value, Type targetType,
object? parameter, CultureInfo culture)
{
if (value is null)
return true;
return false;
}
public object? ConvertBack(object? value, Type targetType,
object? parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
Now, in the ContentView
, I added this XAML
<ldd:LanguageDropdown
x:Name="lddTarget"
BorderColor="{StaticResource RedAlert}"
Placeholder="{lang:Translate DictionaryLanguage}"
SelectedItem="{Binding LanguageTarget}"
SemanticProperties.Description="{lang:Translate DictionaryLanguage}"
TextSize="{StaticResource ElementFontSizeNormal}">
<ldd:LanguageDropdown.Triggers>
<DataTrigger Binding="{Binding LanguageTarget,
Converter={StaticResource LanguageConverter}}"
TargetType="ldd:LanguageDropdown">
<DataTrigger.Value>true</DataTrigger.Value>
<Setter Property="BorderColor"
Value="{StaticResource RedAlert}" />
</DataTrigger>
</ldd:LanguageDropdown.Triggers>
</ldd:LanguageDropdown>
The value SelectedValue
changes when a new item is selected. If not, it is null or empty. Although the SelectedValue
is empty or not, the converter is not called.