Another WPF approach other than BradledDotNet answer is to use triggers. This will be purely XAML based without converter code.
XAML:
<Label>
<Label.Style>
<Style TargetType="Label">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=IsAuthenticated}" Value="True">
<Setter Property="Content" Value="Authenticated" />
</DataTrigger>
</Style.Triggers>
</Style>
</Label.Style>
</Label>
Same rule applies as mentioned by BradledDotNet, ViewModel should be attached and "IsAuthenticated" property should raise the PropertyChanged event.
-- Add some boilerplate code as suggested by Gayot Fow --
View Code Behind:
For simplicity I will just set the DataContext of the view in code behind. For a better design you can use ViewModelLocator as explained here.
public partial class SampleWindow : Window
{
SampleModel _model = new SampleModel();
public SampleWindow() {
InitializeComponent();
DataContext = _model; // attach the model/viewmodel to DataContext for binding in XAML
}
}
Example Model (it should really be ViewModel):
The main point here is the attached model/viewmodel has to implement INotifyPropertyChanged.
public class SampleModel : INotifyPropertyChanged
{
bool _isAuthenticated = false;
public bool IsAuthenticated {
get { return _isAuthenticated; }
set {
if (_isAuthenticated != value) {
_isAuthenticated = value;
OnPropertyChanged("IsAuthenticated"); // raising this event is key to have binding working properly
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
void OnPropertyChanged(string propName) {
if (PropertyChanged != null) {
PropertyChanged(this, new PropertyChangedEventArgs(propName));
}
}
}