c# – DataGridTextColumn value depending on DataGridComboBoxColumn selection

Question:

There is a DataGrid data is filled through binding:

<Custom:DataGrid Name="dgMenuProduct" ItemsSource="{Binding Menus}" AutoGenerateColumns="False">
    <Custom:DataGrid.Columns>
        <Custom:DataGridComboBoxColumn x:Name="ColProduct" SelectedValueBinding="{Binding Path=Product}" ItemsSource="{Binding}"
                                       SelectedValuePath="Prod" DisplayMemberPath="Prod" Header="Продукт" Width="4*"/>
        <Custom:DataGridTextColumn Binding="{Binding Path=CenaProd}"  Header="Цена" Width="2*"/>
        <Custom:DataGridCheckBoxColumn Binding="{Binding Path=Subvencia}" Header="Субвенция" Width="2*"/>
        <Custom:DataGridTextColumn IsReadOnly="False" Binding="{Binding Path=KolvoProd}" Header="Количество" Width="2*"/>
        <Custom:DataGridComboBoxColumn x:Name="ColTip" SelectedValueBinding="{Binding Path=TipPP}" ItemsSource="{Binding}" 
                                       SelectedValuePath="Tip" DisplayMemberPath="Tip" Header="Тип" Width="3*"/>
    </Custom:DataGrid.Columns>
</Custom:DataGrid>

The content of the dropdown is taken from the xml file and loaded into the class:

public class ProductPit
{
    public ProductPit(string prod, double cena, string ediz)
    {
        Prod = prod;
        Cena = cena;
        EdIz = ediz;
    }

    public string Prod { get; set; }
    public double Cena { get; set; }
    public string EdIz { get; set; }
}

The Datagrid itself is bound from the class:

public class Menu
{
    public Menu(string product, double cenaprod, bool subvencia, double kolvoprod, string tippp)
    {
        Product = product;
        CenaProd = cenaprod;
        Subvencia = subvencia;
        KolvoProd = kolvoprod;
        TipPP = tippp;
    }
    // Наименование продукта - будет задаваться в DataGridComboBoxColumn, заполненного  List<ProductPit> months
    public string Product { get; set; }
    // Цена продукта - будет задаваться в DataGrid, в соответствии с выбранным продуктом питания
    public double CenaProd { get; set; }
    public bool Subvencia { get; set; }
    // Количество продукта питания - будет задаваться в DataGrid вручную
    public double KolvoProd { get; set; }
    public string TipPP { get; set; }
}

How to make sure that when a value is selected in the drop-down list of the first column, the corresponding value from the ProductPit class is substituted in the second column?

Answer:

You need to override the Product setter in the Menu class:

string product;
public string Product
{
    get => product;
    set
    {
        product = value;
        CenaProd = ProductPits // - это ваша коллекция, содержащая все ProductPit
            .FirstOrDefault(pp => pp.Prod == product)
            .Cena;
    }
}

Although, I think it would be more correct to store a link to a specific ProductPit in the Menu class, and take Product and CenaProd from it:

public ProductPit ProductPit { get; set; }
public string Product => ProductPit.Prod;
public double CenaProd => ProductPit.Cena;
Scroll to Top