Reputation: 4512
I have DataGrid with 5 columns. Value of some column depends on other columns. how to create this dependency? I have tried to implement this structure, which fills data grid. But it was not updated in the case when other cells were eddited.
public class ColorItem
{
// Constructor
/*
* ColorItem constructor
* Params: color name, color
*/
public ColorItem(string color_name, Color color)
{
ItemName = color_name;
RChanel = color.R;
GChanel = color.G;
BChanel = color.B;
}
// Item name
public string ItemName { get; set; }
// Item color (calculated item)
public Brush ItemColor { get { return new SolidColorBrush(Color.FromRgb(RChanel, GChanel, BChanel)); } }
// Item r chanel
public byte RChanel { get; set; }
// Item g chanel
public byte GChanel { get; set; }
// Item b chanel
public byte BChanel { get; set; }
}
Upvotes: 0
Views: 77
Reputation: 21989
I don't think it's a good idea to use Brush
in ViewModel (talking about MVVM), rather use multiconverter or e.g. Color
+ ColorToSolidBrushConverter
.
But in any case you do not rise notification when you change property, so View doesn't know when to update.
Fixed version:
public class ColorItem : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged([CallerMemberName] string property = "") =>
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(property));
public Brush ItemBrush => new SolidColorBrush(Color.FromRgb(R, G, B));
byte _r;
public byte R
{
get { return _r; }
set
{
_r = value;
OnPropertyChanged(); // this will update bindings to R
OnPropertyChanged(nameof(ItemBrush)); // this will update bindings to ItemBrush
}
}
... // same for G and B
}
Upvotes: 1