如何将datagrid索引作为CellEditEnding的参数

时间:2014-10-14 04:34:51

标签: wpf xaml mvvm

这是我的代码。我想要CellEditEnding事件的datagrid索引但是没有得到它。 我可以将datagrid索引作为参数传递给任何其他方式。

<DataGrid x:Name="dgData" AutoGenerateColumns="False" SelectedIndex="{Binding dgSelectedIndex,UpdateSourceTrigger=LostFocus,Mode=TwoWay }" IsSynchronizedWithCurrentItem="True" 
            ItemsSource="{Binding Collection ,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Width="800" Height="345" Background="Transparent"   CanUserAddRows="True" HorizontalScrollBarVisibility="Visible">                                           
    <DataGrid.Columns>
        <DataGridTextColumn Header="id" Binding="{Binding id, UpdateSourceTrigger=PropertyChanged,Mode=TwoWay,FallbackValue='0'}" Visibility="Hidden" />



        <DataGridTextColumn Header="Description" Binding="{Binding Description}">

        </DataGridTextColumn>

        <DataGridTextColumn Header="Quantity" Width="60" Binding="{Binding quantity,StringFormat=N2,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged,FallbackValue='0'}">

        </DataGridTextColumn>


        <DataGridTextColumn Header="Sub Total" Binding="{Binding sub_total,StringFormat=N2, ConverterCulture=hi-IN,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged,FallbackValue='0'}" Width="90" IsReadOnly="True">

        </DataGridTextColumn>
        <DataGridCheckBoxColumn Header="Status"  Binding="{Binding active,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged }"  IsThreeState="False"/>
    </DataGrid.Columns>
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="CellEditEnding">
            <i:InvokeCommandAction Command="{Binding CellChangedCommand}" 
            CommandParameter="{Binding SelectedItems, ElementName=dgData}" />
        </i:EventTrigger>
    </i:Interaction.Triggers>
</DataGrid>

2 个答案:

答案 0 :(得分:1)

您是否尝试过e.Row.GetIndex()?

public void OnCellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
{
    var index = e.Row.GetIndex();
}

答案 1 :(得分:0)

可以尝试一下:

public void OnCellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
{
    DependencyObject dependencyObject = (DependencyObject)e.EditingElement;
    DataGridCell cell = dependencyObject as DataGridCell;

    while ((dependencyObject != null) && !(dependencyObject is DataGridRow))
    {
         dependencyObject = VisualTreeHelper.GetParent(dependencyObject);
    }

    DataGridRow row = dependencyObject as DataGridRow;
    int rowIndex = row.GetIndex();
}