简体   繁体   English

从另一个窗口访问App.xaml中声明的文本框(WpfNotifyIcon工具提示)

[英]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: 我正在使用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>

To use this, I declare it in MainWindow.xaml.cs : 要使用它,我在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? 如何从另一个窗口访问文本框ChangeThis

The ultimate answer is setting up an event to do so. 最终的答案是设置一个活动来这样做。

The Model-View-View Model pattern is a good way to accomplish this. Model-View-View Model模式是实现此目的的好方法。

Basically, you have a class that implements the INotifyPropertyChanged interface and a two-way data binding between both text boxes and the data source. 基本上,您有一个实现INotifyPropertyChanged接口的类,并且在文本框和数据源之间实现了双向数据绑定

您可以使用FindName方法:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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