简体   繁体   中英

Access a textbox declared in App.xaml (WpfNotifyIcon Tooltip) from another window

I am using WpfNotifyIcon, I have declared it as a resource like this:

<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>

To use this, I declare it in MainWindow.xaml.cs :

    public TaskbarIcon tb;
    public Window1 myWindow;
    public MainWindow()
    {
        InitializeComponent();
        tb = (TaskbarIcon) FindResource("MyNotifyIcon");
    }

How can I access the textbox ChangeThis from another window?

The ultimate answer is setting up an event to do so.

The Model-View-View Model pattern is a good way to accomplish this.

Basically, you have a class that implements the INotifyPropertyChanged interface and a two-way data binding between both text boxes and the data source.

您可以使用FindName方法:

 TextBox txtToChange= tb.FindName("txt_ChangeThis") as TextBox;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM