Reputation: 8448
I have a border wihch I want to change its color depending on a boolean variable. I used the link here to implement a Boolean
to Color
converter.
The code looks like this:
The xaml:
<Border Width="45"
Height="45" CornerRadius="5"
Background="{Binding Path=LivenessActive, Converter={StaticResource BrushColorConverter}}" />
The LivenessActive variable in background:
public bool LivenessActive
{
get { return _livenessActive; }
set
{
_livenessActive = value;
OnPropertyChanged("LivenessActive");
}
}
Where the class has an inheritance to the INotifyPropertyChanged
and has implemented the OnPropertyChanged
event.
The BrushColorConverter.cs:
public class BrushColorConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if ((bool)value)
{
{
return new SolidColorBrush(Colors.GreenYellow);
}
}
return new SolidColorBrush(Colors.DarkGray);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
But I can't fire even the BrushColorConverter
. What I'm doing wrong?
2nd: What about if I want to use it from an another Window?
<Border Width="45" Height="45" CornerRadius="5"
Background="{Binding Path=LivenessActive, Converter={StaticResource BrushColorConverter},
RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type view:MyWindowName}}}" />
I used this same code and it's not finding it.
Upvotes: 3
Views: 5542
Reputation: 33364
To sum up comments since there seems to be nothing wrong with code above it would suggest there is a problem with binding context for
Background="{Binding Path=LivenessActive, Converter={StaticResource BrushColorConverter}}"
You cannot reference one Window
from another Window
. If you have 2 independent Window
s each with Border
that should trigger on the same property change then you set DataContext
of both windows to same instance of view model.
Upvotes: 4