views:

274

answers:

4

Hi, My problem is that I want to access from a page to the properties of a control (button, textblock, label, or a menuitem of the window....) placed in a window. The page is placed into the window. How can I do this? Is there any method to find controls by name in a specific window or page or entire application?

Thanks.

A: 

A Window object has a method FindName() (inherited from FrameworkElement). MSDN: http://msdn.microsoft.com/en-us/library/system.windows.frameworkelement.findname.aspx

If I understand correctly, you'll then need to use reflection to enumerate the control's properties.

Update:

In the XAML you'll have something like this:

ListBox o = this.FindName("myListBox") as ListBox;

In the code behind for the window you could use:

<Window>
    <Grid>
        <ListBox x:Name="myListBox" />
    </Grid>
</Window>

There's no "new" involved in the process.

Scott J
ok, but how can I do what I say in my above answer? thanks.
toni
A: 

OK, but one thing, from the page I am I need to access main window that is open and loaded (Page is into Window). HOw to do that? if WinMain is my window...

        object wantedNode = new 
                      WinMain().FindName("ListBoxName");
        if (wantedNode is ListBox)
        {
             // Following executed if Text element was
                    // found.
            ListBox wantedChild = wantedNode as ListBox;
            wantedChild.IsEnabled = false;
        }

but if I do a new instance of my WinMain Window this is not the one is open and loaded... How can I get the reference to the open, loaded and correct window?

thanks.

toni
A: 

Yes, you are right but this is not what I want. I don't want to access from the code behind of the window to a control of the itself. I want to access to the control (listbox) placed in the window from a code behind of the page.

Perhaps I haven't explained well. I'll try again. First when my application starts, it is displayed a window and into this window, in a frame I load a page. Here I post a code snipet:

WinMain.xaml :

            <Window
              xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"        
          xmlns:local="clr-namespace:GParts"
              xmlns:Microsoft_Windows_Themes="clr-namespace:Microsoft.Windows.Themes;
                      assembly=PresentationFramework.Aero"            
              xmlns:UC="clr-namespace:GParts.UserControls"
              x:Class="GParts.WinMain"
              Title="GParts"    
              WindowState="Maximized"
              Closing="Window_Closing"    
              Icon="/Resources/partsIco.gif"
              x:Name="WMain"
              >

                 <Grid x:Name="LayoutRoot" Margin="0">         

                      <Grid.Resources>
                          <XmlDataProvider x:Key="MenuList"
                           XPath="/Pantallas" Source="/AppData/MnuPantallas.xml" />

                          <...>

                      </Grid.Resources>

                           <...>

                           <ListBox Name="LayoutListBox"
                                    Margin="0" 
                                    DataContext="{Binding Source={StaticResource 
                                 MenuList}, XPath=/Pantallas/Category[1]/Screen}"
                                 ItemsSource="{Binding}" 
                                 SelectionChanged="HandleSelectionChanged"
                                 Style="{DynamicResource MenuListBox}" 
                                 IsSynchronizedWithCurrentItem="True"
                                 SelectedIndex="0"/>

                        <...>

                        <ScrollViewer VerticalAlignment="Stretch" 
                           Margin="0" 
                           ScrollViewer.VerticalScrollBarVisibility="Auto"
                           ScrollViewer.HorizontalScrollBarVisibility="Auto">
                            <Grid DockPanel.Dock="Left" Margin="0" x:Name="frameHost">
                                <Grid.DataContext>
                                    <Binding Source="{StaticResource MenuList}" 
                                               XPath="/Pantallas/Category[1]/Screen"/>
                                </Grid.DataContext>

                                <!-- Here, when window loaded in the following frame 
                                 for first time I load 
                                 PageConsultas.xaml described in the xml file-->

                                <Frame x:Name="_frameHost" Margin="0" Source="{Binding 
                                   XPath=@Path}" Width="Auto"/>

                            </Grid>
                        </ScrollViewer>

                        <...>

                    </Grid>

              </Window>

MnuPantallas.xml contains:

          <?xml version="1.0" encoding="utf-8" ?>
          <Pantallas xmlns="">

             <Category Title="Pantallas">
                 <Screen Path="\Pages\PageConsultas.xaml" Title="Consultas" 
                      Description="Consultas" />
                  <Screen Path="\Pages\PageRutas.xaml"     Title="Fuentes"  
                      Description="Fuentes" />    
             </Category>

          </Pantallas>

When application starts it displays the main window WinMain.xaml and into its frame, it is shown the page PageConsultas.xaml. WinMain.xaml is always loaded, and into it, in a frame, always is loaded a page, PageConsultas.xaml or PageRutas.xaml depends on the user menu selection. Then in a certain point of the time, from the code behind of one of these pages I want to accces to listbox 'LayoutListBox' placed in the window loaded WinMain.xaml in order to enabled/disabled the listbox. To do that I need to obtain the reference of the WinMain.xaml loaded, search the listbox by its name (LayoutListBox), and then accesss property isEnabled and set it to true or false depends on the case.

Thanks.

toni
A: 

I have obtained the window reference using Reflection:

    public static Window WindowByName(string strWindowName)
    {
        if (string.IsNullOrEmpty(strWindowName)) return null;

        Assembly asm = Assembly.GetExecutingAssembly();
        string strFullyQualifiedName = asm.GetName().Name + "." + strWindowName;
        object obj = asm.CreateInstance(strFullyQualifiedName);

        if (obj != null)
            return obj as Window;
        else
            return null;
    }

with the above method I retrieve the reference of WinMain.xaml and then I address its controls by using FindName method from PageRutas.xaml code behind like this:

        // Retrieve reference WinMain.xaml reference
        Window win = cWindow.WindowByName("WinMain");

        // Retrieve listbox reference
        System.Windows.Controls.ListBox lstbox = 
             (System.Windows.Controls.ListBox)win.FindName("LayoutListBox");

        // Modifying IsEnabled property
        lstbox.IsEnabled = false;

but now... I have another problem.... property changed to false after executing lstbox.IsEnable = false; line but something strange is happening because WinMain.xaml UI seems to ignore the change I have made (the listbox doesn't disabled in the UI). What's happening? anybody can help me?

Thanks.

toni