【问题标题】:Get value of a hidden column获取隐藏列的值
【发布时间】:2016-01-21 06:15:15
【问题描述】:

我有一个DataGrid,每行都有一个ID,Name 列和一个HyperLinkButton。我的ID 列被Visibility="Collapsed" 属性隐藏。在 HyperLinkBut​​ton 的 Click 事件中,我正在访问我的列值,但是当它被隐藏时我无法访问该列,即它返回 null。只有当我将其设为Visible 时才能访问它。这个问题有解决办法吗?

DataGrid 的 XAML:

<sdk:DataGrid AutoGenerateColumns="False"
              HorizontalAlignment="Left"
              Height="163"
              VerticalAlignment="Top"
              Name="ProductGrid">
    <sdk:DataGrid.Columns>
        <sdk:DataGridTextColumn x:Name="ID"
                                Binding="{Binding Path=ProductID, Mode=OneWay}"
                                Header="ID"
                                IsReadOnly="True"
                                Width="50"
                                Visibility="Collapsed" />
        <sdk:DataGridTextColumn x:Name="Name"
                                Binding="{Binding Path=Name, Mode=OneWay}"
                                Header="Name"
                                IsReadOnly="True"
                                Width="340" />
        <sdk:DataGridTemplateColumn Header="">
            <sdk:DataGridTemplateColumn.CellTemplate>
                <DataTemplate x:Name="gridTemplate">
                    <StackPanel Orientation="Vertical"
                                VerticalAlignment="Center">
                        <HyperlinkButton Content="Details"
                                         Tag="Hyperlinkbutton"
                                         HorizontalAlignment="Center"
                                         Click="OnGetDetailsClick"
                                         Width="100" />
                    </StackPanel>
                </DataTemplate>
            </sdk:DataGridTemplateColumn.CellTemplate>
        </sdk:DataGridTemplateColumn>
    </sdk:DataGrid.Columns>

HyperLink的点击事件:

var selectedGridRow = DataGridRow.GetRowContainingElement(sender as FrameworkElement);
TextBlock txtblkID = (TextBlock)ProductGrid.Columns[0].GetCellContent(selectedGridRow);
p.ID = Int32.Parse(txtblkID.Text);

【问题讨论】:

  • 你也可以HyperLinkButton XAMl 代码吗?
  • @user2946329 为 DataGrid 添加了 XAMl 代码

标签: c# silverlight datagrid


【解决方案1】:

您可以获取所选行的 DataContext 并从中获取值。将 DataSourceType 转换为您的模型。

var selectedGridRow = DataGridRow.GetRowContainingElement(sender as FrameworkElement);
p.ID = ((DataSourceType)selectedGridRow.DataContext).ProductID;

【讨论】:

  • 我添加了这一行 p.ID = ((DataSourceType)selectedGridRow.DataContext).ProductID; 并得到了错误 The type or namespace name DataSourceType could not be found
  • 是的,正如我在评论中所说,我不知道您的数据模型叫什么,您必须将其替换为您用于网格数据源的任何类型。
  • 怎么样?目前我正在将 Grid 与 GetProductsCompletedEventArgs e.Result 属性 PagedCollectionView pagingCollection = new PagedCollectionView(e.Result); ProductGrid.ItemsSource = pagingCollection; 绑定
  • 您将 ID 列绑定到名为 ProductId 的字段。此产品 ID 是在哪里定义的?
  • 在课堂上Product
猜你喜欢
  • 2021-10-16
  • 2011-05-06
  • 2018-10-08
  • 2019-11-01
  • 2013-06-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-18
相关资源
最近更新 更多