1

I have datagrid, which should allow user add row. But it should add item where one of the field defined by the one of the model properties. Have anybody an idea how to do it. Code like this:

public class OrderWindowModel : BaseModel
{
    public ObservableCollection<GoodM> Goods { get; set; }
    public Service Service { get; set; }
}

public class GoodM : BaseModel
{
    public Service Service { get; set; }
    public List<Good> Goods
    {
        get{ return Service.GoodsL; }
    }

    public Good CurrGood { get; set; }
}

And xaml

<custom:DataGrid Margin="5" Grid.Row ="1" CanUserAddRows="True" CanUserDeleteRows="True" 
            AutoGenerateColumns="False" SnapsToDevicePixels="True" SelectedIndex="0" 
            CanUserReorderColumns="True" ItemsSource="{Binding Goods}" Grid.ColumnSpan="2">
    <custom:DataGrid.Columns>
        <custom:DataGridComboBoxColumn Header="Товар" DisplayMemberPath="{Binding Name}" 
                        SelectedItemBinding="{Binding CurrGood}"  ItemsSource="{Binding Goods}" Width="*">
        </custom:DataGridComboBoxColumn>
    </custom:DataGrid.Columns>
</custom:DataGrid>
har07
  • 88,338
  • 12
  • 84
  • 137
aleshko
  • 385
  • 3
  • 20

1 Answers1

4

You can do that using code behind. Hook to InitializingNewItem event in your XAML:

<DataGrid InitializingNewItem="DataGrid_InitializingNewItem"/>

In the handler you will get NewItem added where you can set the value for your field:

private void DataGrid_InitializingNewItem(object sender,
                                          InitializingNewItemEventArgs e)
{
    if (e.NewItem != null)
    {
       ((GoodM)newItem).Service = // Set value here;
    }
}
Rohit Vats
  • 79,502
  • 12
  • 161
  • 185