从子窗口父窗口呼叫功能 [英] Call Function in Parent Window from Child Window

查看:109
本文介绍了从子窗口父窗口呼叫功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图实现一个WPF程序自定义搜索对话框。 。父窗口是绑定到一个观察的集合一个ListView

I'm attempting to implement a custom search dialog in a WPF program. The Parent window is a ListView that is bound to an Observable Collection.

我做了与搜索形成一个新的窗口,它是像这样初始化:

I've made a new window with the search form and it is initialized like so:

searchForm sf = new searchForm(_QPCollection);
sf.Owner = this;
sf.Show();

和我有这个功能,我想致电(所有者窗口):

and I have this function I am trying to call (in the Owner window):

public void selectIndex(int index)
{
    ListViewItem toSelect = listView1.Items[index] as ListViewItem;
    toSelect.Focus();
}

然后在试图调用selectIndex像这样子窗口(searchForm):

Then in the Child window (searchForm) attempting to call selectIndex like so:

public void SearchJob_Click(object sender, RoutedEventArgs e)
{
    if (sJob.Text == "" || sJob.Text == null) { return; }
    for (int i = findCount; i < _QPCollectionSearch.Count; i++)
    {
        if (i == _QPCollectionSearch.Count - 1) { i = 0; }
        if (_QPCollectionSearch[i].jobNumAndFlow.IndexOf(sJob.Text) > -1)
        {
            findCount = i;
            Owner.selectIndex(i);
        }

    }
}



我得到错误:System.Windows.Window不包含selectIndex的定义,

I get the error: System.Windows.Window does not contain a definition for "selectIndex".

_QPCollection 是观察的集合,搜索将遍历。我有搜索逻辑的工作,但我似乎无法焦点()中的父窗口的ListView的索引。

The _QPCollection is the observable collection that the search will loop through. I have the search logic working, but I cannot seem Focus() the index of the ListView in the Parent Window.

我首先想到的是让我可以在指数传递给它会做的重点,但我似乎无法找到一个方法来调用从子窗口是在父窗口功能的公共职能。

My first thought was to have a public function that I could pass the index to and it would do the focus, but I cannot seem to find a way to call function from the Child Window that are in the Parent Window.

我是不是接近这完全错了吗?这答案似乎是的WinForms,但我相信有访问父窗口,并在WPF其公共职能/性能的方法。

Am I approaching this completely wrong? This answer seems to be for WinForms but I am sure there is a way to access the Parent Window and its public functions/properties in WPF.

推荐答案

处理这种情况将是你的 searchForm 来引发事件的更清洁的方式。父窗口可以侦听事件,并把重点放在自己的列表视图:

A cleaner way of handling that scenario would be for your searchForm to raise an event. The parent window could listen for that event and set the focus on its own list view:

public class searchForm 
{
    public event EventHandler<SearchEventArgs> SearchResultSelected = delegate { };
}

public class SearchEventArgs : EventArgs
{
    public int Index { get; set; }
}

searchForm sf = new searchForm(_QPCollection);
sf.SearchResultSelected += (s, e) => MyListView.SelectedIndex = e.Index;

这篇关于从子窗口父窗口呼叫功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆