This question deals entirely with code and no XAML.
So I have this class, called Location:
public class Location
{
public int id { get; set; }
public double latitude { get; set; }
public double longitude { get; set; }
public string name { get; set; }
public string type { get; set; }
public bool isAnOption { get; set; }
public Location(int newId, double newLatitude, double newLongitude, string newName, string newType)
{
id = newId;
latitude = newLatitude;
longitude = newLongitude;
name = newName;
type = newType;
isAnOption = true;
}
public System.Windows.Shapes.Ellipse createIcon()
{
System.Windows.Shapes.Ellipse icon = new System.Windows.Shapes.Ellipse();
SolidColorBrush brush;
if (isAnOption)
{
brush = new SolidColorBrush(Colors.Blue);
}
else
{
brush = new SolidColorBrush(Colors.Red);
}
brush.Opacity = 0.5;
icon.Fill = brush;
icon.Height = icon.Width = 44;
icon.HorizontalAlignment = HorizontalAlignment.Left;
icon.VerticalAlignment = VerticalAlignment.Top;
Thickness locationIconMarginThickness = new Thickness(0, 0, 0, 0);
locationIconMarginThickness.Left = (longitude - 34.672852) / (35.046387 - 34.672852) * (8704) - 22;
locationIconMarginThickness.Top = (32.045333 - latitude) / (32.045333 - 31.858897) * (5120) - 22;
icon.Margin = locationIconMarginThickness;
Label labelName = new Label();
labelName.Content = name;
StackPanel locationData = new StackPanel();
locationData.Children.Add(labelName);
ToolTip toolTip = new ToolTip();
toolTip.Content = locationData;
icon.ToolTip = toolTip;
return icon;
}
}
Pretty straight forward. Notice the createIcon method.
Now, in the MainWindow (it’s a WPF project), I declare List<Location> locations and fill it up with data.
At some point I put the “icons” on an existing GridScroller like so:
gridScroller.Children.Add(location.createIcon());
Now, the problem I have is I want to bind the property isAnOption of Location to the color of the brush of a corresponding icon’s brush’s color. In other words, when the property isAnOption of a certain object derived from Location is changed, I would like that change to reflect in the color of the ellipse that’s on the GridScroller.
Help please.
Thanks
First, your Location class would need to implement INotifyPropertyChanged, so any bindings that use
isAnOptionas a source will be notified when it is changed.Then you can bind the Fill property to your property like so:
Finally, MyConverter would be a custom IValueConverter that returns a Blue or Red brush based on the bool value passed.