从wpf datagrid获取隐藏值

时间:2013-08-16 06:49:47

标签: c# wpfdatagrid

我是wpf datagrid的新手。我从数据网格中的非隐藏字段获取值,但如果该字段设置为hidden,则返回null值。我将列表绑定到数据网格。

<my:DataGrid RowHeight="30" ColumnWidth="50" AutoGenerateColumns="False" Margin="20,17,0,0" Name="dgUserEnroll" ItemsSource="{Binding}" Background="{StaticResource buttonFlash}" HorizontalAlignment="Left" Width="292" CanUserResizeColumns="False" CanUserAddRows="True" CanUserDeleteRows="True" RowDetailsVisibilityMode="Visible" Height="194" VerticalAlignment="Top">
        <my:DataGrid.Columns>
            <my:DataGridTextColumn Header="id" Width="*" Binding="{Binding Path=UserID}" Visibility="hidden"/>
            <my:DataGridTextColumn Header="UserName" Width="*" Binding="{Binding Path=AdminName}" />
            <my:DataGridTextColumn Header="Role" Width="*" Binding="{Binding Path=RoleName}" />

        </my:DataGrid.Columns>
</my:DataGrid>

C# Code behind:
//To get values from 0th index.
var UsrID = DatagridUtilities.GetCell(this.dgUserEnroll, rowIndex, 0);
user.UserID = int.Parse((UsrID.Content as TextBlock).Text.ToString());

1 个答案:

答案 0 :(得分:1)

不是试图从Cell中读取值,为什么不直接为你的行拉出Databound项呢?

if (dgUserEnroll.SelectedItem != null)
{
  var data = (User)dgUserEnroll.SelectedItem;
  var userID = data.UserId;
 }