【问题标题】:Getting value from SelectedItem RadGridView从 SelectedItem RadGridView 获取价值
【发布时间】:2016-02-21 04:39:01
【问题描述】:

我正在使用这个列表:

List<AssetListData> assetList = new List<AssetListData>();

填充数据,并使用此代码将其绑定到我的RadGridView

AssetList_GridView.ItemsSource = assetList;

现在我有一个带有两列的GridViewNameType)。我在AssetList_GridView 中创建了一个ContextMenu,由EditDelete 组成。单击ContextMenu 后我需要获取该值,但它失败了。我在ContextMenu 的点击事件中尝试了这段代码:

private void GridContextMenu_ItemClick(object sender, Telerik.Windows.RadRoutedEventArgs e)
{
        MenuItem item = (e.OriginalSource as RadMenuItem).DataContext as MenuItem;
        switch (item.Text)
        {
            case "Edit Asset":
                var typeValue = ((assetListData)AssetList_GridView.SelectedItem).assetType;
                this.AssetList_GridView.BeginEdit();
                break;
            case "Delete Asset":
                this.AssetList_GridView.Items.Remove(this.AssetList_GridView.SelectedItem);
                break;
        }
}

来自var typeValue = ((assetListData)AssetList_GridView.SelectedItem).assetType;的错误说:

无法找到资产列表。

为什么我这里不能访问assetList,但我可以访问GridView ItemsSource? 有没有什么简单的方法可以从被点击的行中获取价值?

【问题讨论】:

    标签: c# wpf gridview radgridview


    【解决方案1】:

    SelectedItem 不能重新输入到 LIST,如果你想重新输入,你应该使用

    var typeValue = ((AssetListData)AssetList_GridView.SelectedItem).assetType;
    

    【讨论】:

    • 错误还是一样,我会编辑我的问题以使其更详细
    • ah nvm 我需要写下我之前创建的 AssetListData 事件的所有目录。谢谢
    • 先生,如果我想同时获得assetName和assetType怎么办?如果我有更多并且想要获得该行中的所有价值怎么办?
    • 您可以执行 var row = (AssetListData)AssetList_GridView.SelectedItem 并获得整个 ROW,但不确定这是否可行。否则看stackoverflow.com/questions/5121186/…
    猜你喜欢
    • 1970-01-01
    • 2013-10-08
    • 2011-02-17
    • 1970-01-01
    • 2010-11-17
    • 2012-12-09
    • 2010-12-10
    • 2013-04-25
    • 2017-08-15
    相关资源
    最近更新 更多