如何使用变量引用窗口 [英] How to reference a window using a variable

查看:28
本文介绍了如何使用变量引用窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 SQL 数据库中的数据动态创建了一些窗口,并动态命名了它们.现在我想使用来自已单击标签的数据来引用它们.下面是一个基本示例.

I've created some windows dynamically, and named them dynamically, using data from an SQL database. Now I want to reference them using data from a label that has been clicked on. Below is a basic example.

private void buildWindow(string contentFromDataBase)
{
    Window fooWindow = new Window();
    fooWindow.Name = contentFromDataBase + "Window"
}

//Event handler for a label being clicked
private void showWindow(object sender, EventArgs e)
{
  //Now I want to get access to fooWindow via it's name, which is similar to the label name
  Label foo = sender as Label;
  foo.Name + "Window".show();
}

我该怎么做?

推荐答案

您需要通过 Name<在 Application.Current.Windows 中搜索您的 Window/code> 属性.

You need to search Application.Current.Windows for your Window by it's Name property.

var targetWindow = Application.Current.Windows
    .Cast<Window>()
    .Where(window => window.Name == String.Concat(foo.Name, "Window"))
    .DefaultIfEmpty(null)
    .Single();

if (targetWindow != null)
   targetWindow.Show();

这篇关于如何使用变量引用窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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