Sabuncu
Sabuncu

Reputation: 5264

Access WPF window control from another assembly

I have a library DLL, and a WPF test exe that depends on the said library. Here's the test exe:

using MyLibrary;

namespace WpfTest
{
    public partial class Window2 : Window
    {
        private MyLibrary _mylib;

        public Window2()
        {
            InitializeComponent();
            this.Loaded += window2Loaded;
        }

        void window2Loaded(object sender, RoutedEventArgs e)
        {
            _mylib = new MyLibrary(this);
        }
    }
}

In the MyLibrary class (in the DLL), I have the following constructor:

    public MyLibrary (System.Windows.Window window) // BREAKPOINT HERE.
    {
        ...

At the breakpoint above, the debugger shows the TextBlock (tb_mp) I want to access:

enter image description here

What do i need to do so that when I type window.tb_ in Visual Studio, IntelliSense will offer to complete it as window.tb_mp?

Upvotes: 0

Views: 928

Answers (1)

Dai
Dai

Reputation: 154995

Nothing, you can't. The Locals widnow shows an envelope icon next to the tb_mp local, that means it's an internal member (see here: https://msdn.microsoft.com/en-us/library/y47ychfe(v=vs.100).aspx ). Visual Studio's intellisense window won't list members you don't have access to, in this case, internal members from another project.

There are three options:

  1. Change the access-modifier (in the original project) to public

  2. Use reflection to access the field (e.g. FieldInfo fi = window.GetType().GetField("tb_mp"))

  3. Iterate through controls in a window by using VisualTreeHelper. This is a more complicated topic, discussed at length here: How can I find WPF controls by name or type? and here: WPF: How do I loop through the all controls in a window?

Upvotes: 2

Related Questions