WPF DataGrid - Event for New Rows?

前端 未结 7 1917
梦谈多话
梦谈多话 2020-12-06 17:13

I\'m using the WPF DataGrid (.Net 3.5 SP 1 version from the Toolkit)

What event can I subscribe to, to detect when a new row is added?

相关标签:
7条回答
  • 2020-12-06 18:08

    The event you are looking for is DataGrid.AddingNewItem event. This event will allow you to configure the new object as you want it and then apply it to the NewItem property of the AddingNewItemEventArgs.

    Xaml:

            <DataGrid Name="GrdBallPenetrations"
                  ItemsSource="{Binding BallPenetrationCollection}" 
                  SelectedItem="{Binding CurrentSelectedBallPenetration}"
                  AutoGenerateColumns="False" 
                  IsReadOnly="False"
                  CanUserAddRows="True"
                  CanUserDeleteRows="True"
                  IsSynchronizedWithCurrentItem="True"
                  AddingNewItem="GrdBallPenetrations_AddingNewItem">
    

    Code behind:

    private void GrdBallPenetrations_AddingNewItem(object sender, AddingNewItemEventArgs e)
        {
            e.NewItem = new BallPenetration
            {
                Id              = Guid.NewGuid(),
                CarriageWay     = CariageWayType.Plus,
                LaneNo          = 1,
                Position        = Positions.BetweenWheelTracks
            };
        }
    
    0 讨论(0)
提交回复
热议问题