Note: mm8's answer using DataTrigger's is great if you're developing a WPF application.
In WPF and UWP, you can create a custom implementation of the IValueConverter interface to achieve this with almost the same code. Basically, it will transform your string input to a boolean depending on the rules you define.
WPF:
public class StringToBooleanConverter: IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return value.ToString().Equals("y");
}
// This is not really needed because you're using one way binding but it's here for completion
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if(value is bool)
{
return value ? "y" : "n";
}
}
}
UWP:
The above code is exactly the same except for the last parameter in Convert and ConvertBack methods:
public object Convert(object value, Type targetType, object parameter, string language) { }
public object ConvertBack(object value, Type targetType, object parameter, string language) { }
The following is more or less the same for both WPF and UWP. You can use the converter in XAML to convert from string to bool:
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="IsSelected" Value="{Binding f_selected, Mode=OneWay, Converter={StaticResource StringToBooleanConverter}}" />
</Style>
</ListBox.ItemContainerStyle>
Also, remember to introduce the converter in the beginning:
<Window.Resources>
<local:YesNoToBooleanConverter x:Key="StringToBooleanConverter" />
</Window.Resources>