C#WPF - 将click事件的值传递给其他类文件

时间:2015-11-23 04:13:39

标签: c# wpf contextmenu radgridview

我使用的RadGridView有11行和多列,我只显示2列,即Name和Type。我为行创建了ContextMenu,如果点击它,将会打开新窗口。假设我在columnType中有3个不同的值,即A,B,C。我有ContextMenu的点击事件,如下所示:

public void GridContextMenu_ItemClick(object sender, Telerik.Windows.RadRoutedEventArgs e)
{
    MenuItem item = (e.OriginalSource as RadMenuItem).DataContext as MenuItem;
    typeValue = (GridView.SelectedItem).columnType;
    switch (item.Text)
    {
        case "Edit]":
            if (typeValue == "A")
            {
                selectedItem = GridView.SelectedItem;
                editA.Show();
                this.Close();
                break;
            }
            else if (typeValue == "B")
            {
                selectedItem = GridView.SelectedItem;
                editB.Show();
                this.Close();
                break;
            }
            else if (typeValue == "C")
            {
                selectedItem = GridView.SelectedItem;
                editC.Show();
                this.Close();
                break;
            }
            break;
        case "Delete":
            this.GridView.Items.Remove(this.GridView.SelectedItem);
            break;
    }
}

所以我可以得到selectedItem.columnTypeselectedItem.column3。但问题是,我不知道如何将selectedItem值传递给刚刚打开的窗口?我为{ get;set; }尝试了selectedItem静态类,但是当我尝试从新窗口调用它时,值仍然为空。

1 个答案:

答案 0 :(得分:3)

您可以为image/png创建参数化构造函数,并在Window实例化中传递SelectedItem值。像这样:

Window

并像这样使用它:

public partial class EditWindow : Window
{    
    public EditWindow()
    {
      InitializeComponent();
    }

    public EditWindow(YourObjectType selectedItem) : this()
    {
      var yourSelectedItem = selectedItem;
    }
}