Search code examples
wpfdatagridcomboboxcolumn

WPF DataGridComboBoxColumn binding


im having some trouble when trying to use a DataGridComboBoxColumn to update my entity framework

I have a datagrid that I am binding to a Custom Model (FunctionPrinterLookupModel), which is basically a lookup between Printers and Functions around the building. The Functions are all static, but I would like users to be able to select which printer they use for the function.

<DataGrid Grid.Row="1" x:Name="gridLookup" AutoGenerateColumns="False" Width="500" RowEditEnding="gridLookup_RowEditEnding" Margin="20">
                    <DataGrid.DataContext>
                        <Models:Printer/>
                    </DataGrid.DataContext>
                    <DataGrid.Columns>
                        <DataGridTextColumn Header="Function" Width="*" IsReadOnly="True" Binding="{Binding FunctionName}"/>
                        <!--<DataGridTextColumn Header="Printer" Width="*" Binding="{Binding PrinterName, UpdateSourceTrigger=PropertyChanged}"/>-->
                        <DataGridComboBoxColumn x:Name="ddlPrinters" Header="Printer" Width="*"  SelectedValueBinding="{Binding PrinterID, Mode=TwoWay}" SelectedValuePath="{Binding PrinterID, Mode=TwoWay}" DisplayMemberPath="{Binding PrinterName}"/>
                    </DataGrid.Columns>
                </DataGrid>

 private void gridPrinters_RowEditEnding(object sender, DataGridRowEditEndingEventArgs e)
    {
        if (e.EditAction == DataGridEditAction.Commit)
        {
            Printer printer = (Printer)e.Row.Item;
            if (printer.PrinterID != 0)
            {
                Printer printerDB = context.Printers.Where(s => s.PrinterID == printer.PrinterID).Single();
                printerDB.PrinterName = printer.PrinterName;
                context.SaveChanges();
            }
            else
            {
                Printer newPrinter = new Printer()
                {
                    PrinterName = printer.PrinterName
                };
                context.Printers.Add(newPrinter);
                context.SaveChanges();
            }
        }

        RefreshPrintersGrid();
    }

I am binding the DataGridComboBoxColumn in the code behind to an EF model containing list of Printers.

When the value has been selected and we trigger the RowEditEnding function, the value of the combobox is not updated in the FunctionPrinterLookupModel model. I feel like im tying myself in knots here and havent been able to find a solution that works from my hours of googling. Can any one help straighten me out?


Solution

  • You would be better off binding the combobox items source to a property in your ViewModel. Then bind the selected printer and in the ViewModel take action when the property changes.

    <DataGridComboBoxColumn x:Name="ddlPrinters" Header="Printer" Width="*" ItemsSource="{Binding PrinterList}"  SelectedItem="{Binding SelectedPrinter, Mode=TwoWay}" SelectedValuePath="PrinterID" DisplayMemberPath="PrinterName"/>
    

    In ViewModel

    Private PrinterInfo _SelectedPrinter { get; set; }
    
    Publuc PrinterInfo SelectedPrinter 
    {
        get
        {
            return _SelectedPrinter;
        }  
        set
        {
            _SelectedPrinter = value;
            //save value to db or other actions
        }  
    }