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

时间:2013-04-03 16:14:33

标签: c# .net wpf xaml notifyicon

我正在使用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

2 个答案:

答案 0 :(得分:0)

最终的答案是建立一个这样做的事件。

Model-View-View Model模式是实现此目标的好方法。

基本上,你有一个实现INotifyPropertyChanged接口的类和两个文本框和数据源之间的双向data binding

答案 1 :(得分:0)

您可以使用FindName方法:

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