0

Hi i have problem with accessing hidden value in DataGrid of selected 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>

This is code witch I use to do that, but I got that element doesnt exist.

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));
}

Is there a simply way to do that?

EDIT:

row["ID"] is not SelectedItem.id. This is diffrent value.

4

1 回答 1

1

您应该将此逻辑移至视图模型。后面代码中的事件处理程序应如下所示:

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" >
于 2013-06-30T21:23:56.730 回答