Elad770
Member
- Joined
- Oct 4, 2021
- Messages
- 20
- Programming Experience
- 1-3
Hello, I have an Enum of statuses and I want to color an Ellipse according to a certain status value.
The solution I made is to create a binding in the code according to the variable of the Enum and with Converter I check the status and then return the appropriate color.
The problem with this is that I have many values of statuses in the Enum and I don't want the converter to be activated on every change of status, I want it to be activated on two specific statuses only.
I have several threads in the program that run and update the status every time.
I saw all kinds of solutions on Stack OverFlow but quite complex and they are not exactly related to my problem.
The solution I made is to create a binding in the code according to the variable of the Enum and with Converter I check the status and then return the appropriate color.
The problem with this is that I have many values of statuses in the Enum and I don't want the converter to be activated on every change of status, I want it to be activated on two specific statuses only.
I have several threads in the program that run and update the status every time.
I saw all kinds of solutions on Stack OverFlow but quite complex and they are not exactly related to my problem.
C#:
//Enum Status
public enum Status
{
CREATION,
COLLECTION,
BRANCH_STORAGE,
HUB_TRANSPORT,
HUB_STORAGE,
BRANCH_TRANSPORT,
DELIVERY,
DISTRIBUTION,
DELIVERED
}
//in code behind of MainWindow
public Ellipse CreateBindingEllipse(Package pack)
{
Ellipse el = new Ellipse();
bool isEllipeDown = true;
el.SetBinding(Ellipse.FillProperty, new Binding()
{
Source = pack,
Converter = new PackageElipseConverter(),
UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged,
//this is work well
Path = new PropertyPath("Status"),
////---- I'm want make like Status.CREATION or Status.DELIVERED
//but not working
ConverterParameter = isEllipeDown
});
return el;
}
//Converter
internal class PackageElipseConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
bool isEllipeDown = bool.Parse(parameter.ToString());
Status sta = (Status)Enum.Parse(typeof(Status), value.ToString());
if (isEllipeDown && sta==Status.DELIVERED || !isEllipeDown && sta == Status.CREATION)
{
return (SolidColorBrush)new BrushConverter().ConvertFrom("#B00000");
}
return (SolidColorBrush)new BrushConverter().ConvertFrom("#fc9595");
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return value;
}
//Class Package which contains property Status
//and threads update this Status during the program
using PropertyChanged;
public abstract class Package: INotifyPropertyChanged
{
public int PackageID { get; }
static int count = 1000;
public Address SenderAddress { set; get; }
public Address DestinationAddress { set; get; }
public Priority Priority { set; get; }
public ObservableCollection<Tracking> Tracking { get; }
public Status Status { set; get; }
}
Last edited: