在WPF中从另一个窗口管理用户控件 [英] Managing user controls from another window in WPF
问题描述
我正在使用WPF,并且有一个MainWindow.xaml,我也有一个用户控件Page_SwitcherMenu.xaml,实际上这对于其他用户控件来说很常见。
我在Page_SwitcherMenu.xaml中有一个监控网络活动的标签,如果局域网已关闭,标签背景变为红色。
我需要在MainWindow.xaml.cs中实现代码,标签名称为lbl_LAN,位于Page_SwitcherMenu.xaml。
我该怎么做?我试图使用绑定但没有设法。
我尝试过的事情:
MainWindow.xaml.cs包含以下内容:
public MainWindow()
{
InitializeComponent();
this .WindowStyle = WindowStyle.None;
此 .AllowsTransparency = false ;
PageSwitcher.pageSwitcher = 此;
PageSwitcher.Switch( new Page_Info());
NetworkChange.NetworkAvailabilityChanged + = new NetworkAvailabilityChangedEventHandler(NetworkChange_NetworkAvailabilityChanged);
}
void NetworkChange_NetworkAvailabilityChanged( object sender,NetworkAvailabilityEventArgs e)
{
GlobalData.NetworkOnline = e.IsAvailable;
if (GlobalData.NetworkOnline == true )
< b> lbl_LAN .Background = Brushes.PaleGreen;
else
lbl_LAN .Background = Brushes.Red;
}
由于这个问题非常受欢迎,我以前的答案往往不太清楚,可能不太清楚,我决定写一篇提示/技巧文章,详细的代码示例和解释:许多问题一次回答 - Windows窗体或WPF Windows之间的协作。
-SA
I am using WPF, and have a MainWindow.xaml, also I have a user control Page_SwitcherMenu.xaml, practically this is common for other user controls.
I have a label in Page_SwitcherMenu.xaml which monitors network activity, if LAN is down label background turns red.
I need to implement the code in MainWindow.xaml.cs, the label name is lbl_LAN and is located in Page_SwitcherMenu.xaml.
How can I do this? I have tried to use binding but did not manage to.
What I have tried:
MainWindow.xaml.cs contains the following:
public MainWindow()
{
InitializeComponent();
this.WindowStyle = WindowStyle.None;
this.AllowsTransparency = false;
PageSwitcher.pageSwitcher = this;
PageSwitcher.Switch(new Page_Info());
NetworkChange.NetworkAvailabilityChanged += new NetworkAvailabilityChangedEventHandler(NetworkChange_NetworkAvailabilityChanged);
}
void NetworkChange_NetworkAvailabilityChanged(object sender, NetworkAvailabilityEventArgs e)
{
GlobalData.NetworkOnline = e.IsAvailable;
if (GlobalData.NetworkOnline == true)
lbl_LAN.Background = Brushes.PaleGreen;
else
lbl_LAN.Background = Brushes.Red;
}
As the question turned out to be very popular, and my previous answers often were not well understood, probably were not clear enough, I decided to write a Tips/Trick article complete with detailed code samples and explanations: Many Questions Answered at Once — Collaboration between Windows Forms or WPF Windows.
—SA
这篇关于在WPF中从另一个窗口管理用户控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!