[英]How to Access Window Object declared in App.xaml in class file in WPF?
[英]Access a textbox declared in App.xaml (WpfNotifyIcon Tooltip) from another window
我正在使用WpfNotifyIcon,我已将其声明为这样的资源:
<Application x:Class="NotifyIconScratchPad2.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:tb="http://www.hardcodet.net/taskbar"
StartupUri="MainWindow.xaml">
<Application.Resources>
<tb:TaskbarIcon x:Key="MyNotifyIcon" IconSource="Icons/stopwatch_start.ico" ToolTipText="Hello world" >
<tb:TaskbarIcon.TrayToolTip>
<TextBlock x:Name="ChangeThis" Text="Hello world" />
</tb:TaskbarIcon.TrayToolTip>
</tb:TaskbarIcon>
</Application.Resources>
</Application>
要使用它,我在MainWindow.xaml.cs
声明它:
public TaskbarIcon tb;
public Window1 myWindow;
public MainWindow()
{
InitializeComponent();
tb = (TaskbarIcon) FindResource("MyNotifyIcon");
}
如何从另一个窗口访问文本框ChangeThis
?
最终的答案是设置一个活动来这样做。
Model-View-View Model模式是实现此目的的好方法。
基本上,您有一个实现INotifyPropertyChanged接口的类,并且在文本框和数据源之间实现了双向数据绑定 。
您可以使用FindName方法:
TextBox txtToChange= tb.FindName("txt_ChangeThis") as TextBox;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.