Datagrid从所选行中获取隐藏单元格

时间:2013-06-30 21:09:45

标签: c# wpf .net-4.0 wpfdatagrid

我在访问所选DataGrid的{​​{1}}中的隐藏值时遇到问题:

Row

这是我用来做这个的代码,但我得到的元素不存在。

<DataGrid Name="grid" ItemsSource ="{Binding}" IsReadOnly="True" AutoGenerateColumns="False">
    <DataGrid.Columns>
        <DataGridTextColumn Header="Id_user" Binding="{Binding id_user}" Visibility="Hidden"/>
        <DataGridTextColumn Header="A" Binding="{Binding A}" />
        <DataGridTextColumn Header="B" Binding="{Binding B}" />
    </DataGrid.Columns>

    <DataGrid.ItemContainerStyle>
        <Style TargetType="DataGridRow">
            <EventSetter Event="MouseDoubleClick" Handler="selectRow"/>
        </Style>
    </DataGrid.ItemContainerStyle>
</DataGrid>

有简单的方法吗?

修改

private void selectRow(object sender, RoutedEventArgs e) { DataRowView row = (DataRowView)grid.SelectedItems[0]; var x = row["Id_user"]; this.NavigationService.Navigate(new PageA((string)x)); } 不是row["ID"]。这是不同的

1 个答案:

答案 0 :(得分:1)

您应该将此逻辑移至viewmodel。代码隐藏中的事件处理程序应如下所示:

private void selectRow(object sender,RoutedEventArgs  e) 
{
    ViewModel.Navigate();
}

这是Navigate()实施:

public void Navigate()
{
    NavigationService.Navigate(new PageA(SelectedItem.id_user));
}

剩下要做的就是将视图模型上的SelectedItem属性绑定到DataGrid

<DataGrid Name="grid" SelectedItem="{Binding SelectedItem}" ItemsSource ="{Binding Collection}"  IsReadOnly="True" AutoGenerateColumns="False" >