Since we are using WPF, I would use a binding and converter to accomplish this:
<Page.Resources>
<local:BoolToAuthenticationStringConverter x:Key="BoolToAuthenticationStringConverter"/>
</Page>
<Label Content="{Binding Path=IsAuthenticated, Converter={StaticResource BoolToAuthenticationStringConverter}"/>
Then a converter that looks like:
public class BoolToAuthenticationStringConverter : IValueConverter
{
public object Convert (...)
{
if ((bool)value)
return "Authenticated!";
else
return "Not Authenticated!";
}
public object ConvertBack (...)
{
//We don't care about this one for this converter
throw new NotImplementedException();
}
}
In our XAML, we declare the converter in the resources section, then use it as the "Converter" option of the binding for "Content". In the code, we cast value
to a bool (IsAuthenticated
is assumed to be a bool in your ViewModel), and return an appropriate string. Make sure your ViewModel implements INotifyPropertyChanged
and that IsAuthenticated
raises the PropertyChanged
event for this to work!
Note: Since you won't be changing the Label
via the UI, you don't need to worry about ConvertBack
. You could set the mode to OneWay
to make sure that it never gets called though.
Granted, this is very specific to this scenario. We could make a generic one:
<Label Content="{Binding Path=IsAuthenticated, Converter={StaticResource BoolDecisionToStringConverter}, ConverterParameter='Authenticated;Not Authenticated'}"/>
Then a converter that looks like:
public class BoolDecisionToStringConverter : IValueConverter
{
public object Convert (...)
{
string[] args = String.Split((String)parameter, ';');
if ((bool)value)
return args[0];
else
return args[1];
}
public object ConvertBack (...)
{
//We don't care about this one for this converter
throw new NotImplementedException();
}
}