wpf-窗体传值等

启动时显示位置

如果父窗口是 topmost 窗口,子窗口会被挡住,设成 CenterOwner 就可以了.

private void btnLogin_Click(object sender, RoutedEventArgs e)
{
Type type = this.GetType();
Assembly assembly = type.Assembly;
Window window = (Window)assembly.CreateInstance(type.Namespace + "." + "WinUserLogin");
((WinUserLogin)window).parentFunc = GetChildWinValue;
window.WindowStartupLocation = WindowStartupLocation.CenterOwner;
window.Owner = this;
window.Show();
}

启动时隐藏

<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" ShowInTaskbar="False" Visibility="Hidden">
<Grid>

</Grid>
</Window>

任务栏显示

ShowInTaskbar="False"

最小化,恢复

WindowState = WindowState.Minimized;
WindowState = WindowState.Normal;

if (WindowState == WindowState.Minimized)
{
WindowState = WindowState.Normal;
}

打开另一个 windows

方法一

在打开窗口里面获取全局的 winmain 然后执行相应操作

// 检查登录信息
if (FLAG_LOGIN_OPEN != (flag & FLAG_LOGIN_OPEN))
{
flag |= FLAG_LOGIN_OPEN;
Type type = this.GetType();
Assembly assembly = type.Assembly;
Window window = (Window)assembly.CreateInstance(type.Namespace + ".win." + "WinLogin");
// 窗口保持最前端显示
window.Owner = this;
// 隐藏任务栏图标
window.ShowInTaskbar = false;
window.Show();
}

方法二

// 回调函数
public void CBWinConfigValue(List<string> strValue)
{

}

// 窗口类
public partial class telesisConfig : Window
{
//声明委托
public delegate void TransValueFunc(List<string> strValue);
//委托对象
public TransValueFunc transValueFunc;

public telesisConfig()
{
InitializeComponent();
}
}

Type type = this.GetType();
Assembly assembly = type.Assembly;
Window window = (Window)assembly.CreateInstance(type.Namespace + "." + "WinConfig");
((WinConfig)window).transValueFunc = CBWinConfigValue;
window.WindowStartupLocation = WindowStartupLocation.CenterOwner;
window.Owner = this;
window.Show();

多个 window 的 z-order

传值

// 主窗口
public void TransWinValue(List<string> strValue)
{
string t = strValue[0];
}

private void btnLogin_Click(object sender, RoutedEventArgs e)
{
Type type = this.GetType();
Assembly assembly = type.Assembly;
Window window = (Window)assembly.CreateInstance(type.Namespace + "." + "WinUserLogin");
((WinUserLogin)window).transValueFunc = TransWinValue;
window.WindowStartupLocation = WindowStartupLocation.CenterOwner;
window.Owner = this;
window.Show();
}

// 子窗口
//声明委托
public delegate void TransValueFunc(List<string> strValue);
//委托对象
public TransValueFunc transValueFunc;

private void btnLogin_Click(object sender, RoutedEventArgs e)
{
string user = tb_userName.Text;
string password = tb_password.Password.Trim();
List<string> strParam = new List<string>();
strParam.Add(user);
strParam.Add(password);
transValueFunc(strParam);
Close();
}

判断当前窗体对象

if (((MRLGauger.MainWindow)mainwin).grid_main.Children[0] is MRLGauger.uc.UCMeasureDisplay)
{

}