【问题标题】:Getting value from SelectedItem to other method从 SelectedItem 获取价值到其他方法
【发布时间】:2015-11-20 09:26:03
【问题描述】:

所以我有 RadGridView 和 ContextMenu 有 ItemClick 事件与此代码:

ListData selectedItem = new ListData();    
public void GridContextMenu_ItemClick(object sender, Telerik.Windows.RadRoutedEventArgs e)
    {
        MenuItem item = (e.OriginalSource as RadMenuItem).DataContext as MenuItem;
        switch (item.Text)
        {
            case "Edit":
                selectedItem = (GridView.SelectedItem);
                editMenu.Show();
                this.Close();
                break;
            case "Delete":
                this.GridView.Items.Remove(this.GridView.SelectedItem);
                break;
        }
    }

ListData 是这样的:

public class ListData
{
    public string Name { get; set; }
    public string Type { get; set; }
    ...
    public string Something { get; set; }
}

我知道selectedItem inside case"Edit" 有一行数据,来自名称、类型、...,当我单击“编辑”菜单时,单击的行中的某些内容。但是我如何在同一个 .xaml.cs 甚至其他文件中的其他方法中使用selectedItem?

【问题讨论】:

  • “但是我如何在同一个 .xaml.cs 甚至其他文件中的其他方法中使用 selectedItem?” - 这很模糊,你能提供任何详细了解您想要达到的目标?
  • 如果 selectedItem 在类的顶部声明,它应该在其他方法使用的范围内。另外ListData 与AssetListData 不同。确保它们的拼写方式相同。
  • @dev1998 抱歉回复晚了,是的,我将它编辑为 ListData。我试图在类的顶部声明它。但是当我在另一个类中调用它时,它的值为空

标签: c# wpf variables row radgridview


【解决方案1】:

好的,我不确定这是否正是您需要的,但这对我有用。

在班级顶部我有这个:public ListData selectedItem;

在类构造函数中我有这个:selectedItem = new ListData(); 还有这个public ObservableCollection<ListData> OCLData;

我创建了一个名为 dgMirza 的数据网格,并设置 ItemsSource = OCLData。 OCLData 是 ListData 对象的集合。

在 SelectedCellsChanged 事件中我放了这个:selectedItem = (ListData)dgMirza.SelectedItem;

即使在其他方法中,我也能看到 selectedItem 对象中的数据。

【讨论】:

    猜你喜欢
    • 2016-02-21
    • 1970-01-01
    • 1970-01-01
    • 2020-07-18
    • 2016-05-13
    • 1970-01-01
    • 2013-10-08
    • 2022-11-23
    • 1970-01-01
    相关资源
    最近更新 更多