A small example showing the idea of reliz. Suppose we have 2 fields for the name and surname, and in the third they will be displayed together. First, create a VM-ку for the displayed data.
class ItemVm : INotifyPropertyChanged { private string _firstName; private string _lastName; public string FirstName { get { return _firstName; } set { _firstName = value; OnPropertyChanged(nameof(FullName)); } } public string LastName { get { return _lastName; } set { _lastName = value; OnPropertyChanged(nameof(FullName)); } } public string FullName { get { return $"{FirstName} {LastName}"; } } public event PropertyChangedEventHandler PropertyChanged; protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } }
And the main Vm , which will store the collection of this data and bind to the DataGrid :
class MainVm { public ObservableCollection<ItemVm> ItemVms { get; set; } = new ObservableCollection<ItemVm>(); }
Now the main Vm tied to the window in app.xaml.cs (do not forget to delete the line with StartupUri in StartupUri ):
protected override void OnStartup(StartupEventArgs e) { base.OnStartup(e); new MainWindow() { DataContext = new MainVm()}.Show(); }
Finally, the DataGrid itself:
<Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="clr-namespace:WpfApplication1" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" Title="MainWindow" Width="525" Height="350" d:DataContext="{d:DesignInstance local:MainVm, d:IsDesignTimeCreatable=True}" mc:Ignorable="d"> <Grid> <DataGrid AutoGenerateColumns="True" CanUserAddRows="True" ItemsSource="{Binding ItemVms}" /> </Grid>
This is the result:

UPDATE. To create a DataGrid programmatically and bind data to it you need to do this:
var dataGrid = new DataGrid() { Columns = { new DataGridTextColumn() { Header = "First Name", Binding = new Binding("FirstName") }, new DataGridTextColumn() { Header = "Last Name", Binding = new Binding("LastName") }, new DataGridTextColumn() { Header = "Full Name", Binding = new Binding("FullName") }}, AutoGenerateColumns = false }; this.Content = dataGrid; ObservableCollection<ItemVm> items = new ObservableCollection<ItemVm>(); dataGrid.ItemsSource = items;
Just replace your code with this one:
var m_dictionary_data = new DataGrid() { AutoGenerateColumns = false, CanUserAddRows = true }; this.Content = m_dictionary_data; { m_dictionary_data.Columns.Add(new DataGridTextColumn() { Header = "Color name", Binding = new Binding("ItemName") }); m_dictionary_data.Columns.Add(new DataGridTextColumn() { Header = "Color", Binding = new Binding("ItemColor") }); m_dictionary_data.Columns.Add(new DataGridTextColumn() { Header = "R", Binding = new Binding("RChanel") }); m_dictionary_data.Columns.Add(new DataGridTextColumn() { Header = "G", Binding = new Binding("GChanel") }); m_dictionary_data.Columns.Add(new DataGridTextColumn() { Header = "B", Binding = new Binding("BChanel") }); } ObservableCollection<ColorItem> color_records = new ObservableCollection<ColorItem>() { }; // Filling color_records m_dictionary_data.ItemsSource = color_records;
And ColorItem :
public class ColorItem : INotifyPropertyChanged { private byte _rChanel; private byte _gChanel; private byte _bChanel; // Constructor /* * ColorItem constructor * Params: color name, color */ public ColorItem() { } 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 (dependable item) public Brush ItemColor { get { return new SolidColorBrush(Color.FromRgb(RChanel, GChanel, BChanel)); } } // Item r chanel public byte RChanel { get { return _rChanel; } set { _rChanel = value; OnPropertyChanged(nameof(ItemColor)); } } // Item g chanel public byte GChanel { get { return _gChanel; } set { _gChanel = value; OnPropertyChanged(nameof(ItemColor)); } } // Item b chanel public byte BChanel { get { return _bChanel; } set { _bChanel = value; OnPropertyChanged(nameof(ItemColor)); } } public event PropertyChangedEventHandler PropertyChanged; protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } }