![](/img/trans.png)
[英]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.