[英]set variables from the window frame is now visible, WPF c#
我正在制作一個應用程序,在該應用程序中我會在不同時間打開多個窗口,每個窗口都有一個框架,該框架根據單擊幾頁而合並。
我需要的是從頁面中獲取MainWindow的變量,該變量由框架構建。
這樣做:
public partial class SearchPage : Page
{
public SearchPage()
{
InitializeComponent();
Window mainWindow = Application.Current.MainWindow;
}
}
但是,它需要打開第一個窗口,而不是當前窗口,而是使我始終對程序啟動保持打開狀態的窗口。 無法獲取可見時間的父窗口? 我將隔片關閉到Visibility.Hidden();
並重新打開Visibility.Visible();
我正在使用mahapps metroWindow
不確定是否正是您需要的,抱歉,我需要更多代碼。 在這里,我試圖做到與您所做的相同(我認為)。 如果我誤會了,請糾正我。
MainWindow.xaml
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Main Window" Height="350" Width="525" Loaded="Window_Loaded">
<Grid>
<StackPanel>
<TextBox x:Name="textbox" Text="test" TextChanged="textbox_TextChanged"></TextBox>
<Frame x:Name="fr">
</Frame>
</StackPanel>
</Grid>
MainWindow.xaml.cs
using System;
using System.Windows;
namespace WpfApplication2
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
fr.Navigate(new Uri("Page1.xaml", UriKind.Relative));
}
private void textbox_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
{
if (fr != null) fr.Refresh();
}
}
}
Page1.xaml
<Page x:Class="WpfApplication2.Page1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300"
Title="Page1">
<Grid>
<StackPanel>
<TextBlock>MY PAGE</TextBlock>
<TextBlock>The text in window is: </TextBlock>
<TextBlock x:Name="tb"></TextBlock>
</StackPanel>
</Grid>
Page1.xaml.cs
using System.Windows.Controls;
namespace WpfApplication2
{
public partial class Page1 : Page
{
public Page1()
{
InitializeComponent();
MainWindow w = (MainWindow) App.Current.MainWindow;
tb.Text = w.textbox.Text;
}
}
}
您可以在可視化樹中查找相應的MetroWindow
實例:
var window = this.TryFindParent<MetroWindow>();
TryFindParent
是中定義的擴展方法MahApps.Metro.Controls.TreeHelper
,所以你必須添加using MahApps.Metro.Controls;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.