0

Problem. I don't know how to access the objects inside the passed Window Object from my method.

void MyMethod(Window myWindow){
     myWindow. <--- "I have an Progress bar inside this object that I believe has been passed. But don't know how to access it".




}

I can also try to use the current Application class library from System.Windows but the problem is still the same, I just don't know how to reference the objects that is inside the window.

void MyMethod(){
     ProgressBar myprogressbarInsidetheApp = Application.Current.MainWindow <----- I am lost. Please help.

}
Aizen
  • 1,807
  • 2
  • 14
  • 28
  • the API QR made by other company, needs to have this object sent. I know I am not suppose to do that, but the API is forcing us to. Because the machine have their own implementation of things we can't control. Well the other way that I asked, is pretty much the same on what you are trying to imply. Application.Current – Aizen Jan 18 '16 at 02:27

2 Answers2

1

I'd avoid passing a reference to the Window, but if you have some constraint that's forcing you too, then you'll need to cast it to your specific Window before you can access any elements on it.

If this was your window, named "MainWindow":

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <ProgressBar Name="MyProgressBar" Value="30" />
    </Grid>
</Window>

You'd have to cast it like this:

void MyMethod(Window myWindow)
{
    var pb = ((MainWindow)myWindow).MyProgressBar;

    // do something with "pb"
}
Grant Winney
  • 65,241
  • 13
  • 115
  • 165
  • Thanks, Grant. How about accessing it the proper way or using the Application.Current way? As an update of the answer? – Aizen Jan 18 '16 at 03:53
  • make sense. On a regular part of programming. But I am hindered with this API problem. Thank you for the answer Grant. – Aizen Jan 18 '16 at 04:52
0

You really shouldn't be doing this but if you absolutely insist...use the WPF Tree Visualizer to determine the name of the child element you are trying to access and then use window.FindName("the_elements_name"); to get a reference object. If the element doesn't have a name then you'll need to navigate the entire visual tree looking for an element of the required type.

Community
  • 1
  • 1
Mark Feldman
  • 15,731
  • 3
  • 31
  • 58