.NET Framework Bookmark and Share   
 index > Windows Presentation Foundation (WPF) > access a named xaml element in c# from a window added as a resource.
 

access a named xaml element in c# from a window added as a resource.

Hi, new to this xaml c# milarky. Imagine this is very basic but I have a xaml window to which I've added another xaml object as a resource:

in Window1.xaml

<Key:MainThing DockPanel.Dock="Top" Loaded="MainThing_Loaded"/>

Is it possible for me to access an element of the MainThing by its name, declared in MainThing.xaml:
<it:TheThing Name="theIt">

so I can perform a method on it in C# from from Window1.xaml.cs
Ulysses31
You can acces through binding:

{Binding Property, ElementName=theIt}

http://weblogs.asp.net/marianor/
Mariano O. Rodriguez
Hi, thanks for the quick response but I'm a little confused how to use this (I did say I was new to this xaml). Do I just access the element as I would have normaly if I bind the element name?
Ulysses31

You can access named items directly in code. Resources need keys and you need to lookup the resource by key before you can use it in code. The following example shows both approaches.

<Window
    x:Class="MyApp.MyWindow"
    xmlns="http://schemas.microsoft.com/netfx/2007/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Window.Resources>
        <SolidColorBrush x:Key="MyResource" Color="#FFA0A0A0" />
    </Window.Resources>
    <Grid x:Name="myGrid" x:FieldModifier="private">
    </Grid>
</Window>

namespace MyApp
{
    public partial class MyWindow : Window
    {
        public MyWindow()
            : base()
        {
            InitializeComponent();

            // Can refer to myGrid directly here.
            myGrid.AllowDrop = true;

            // Need to lookup MyResource.
            var brush = (SolidColorBrush)FindResource("MyResource");
        }
    }
}

HTH.

  • Edited bydekurver Tuesday, September 22, 2009 5:03 PMFormatting
  •  
dekurver
Ah right, so in my xaml I use <Key:MainThing DockPanel.Dock="Top" Loaded="MainThing_Loaded"/>
to load the item into my main.xaml window. The MainThing.xaml has within it the created elementof a component which contains a method which I want to perform upon it from the main windows code behind (contained in another directory in the project). In order to do this I need the main.xaml windows code behind cs to access it by its given name property when its declared in the MainThing.xaml.

In order to do this then I can FindResource the name of the element created in the MainThing.xaml which would then allow my main.cs to call the method defined within the component?
Ulysses31
Hmm, I tried this but to no avail. Im sure Im confusing my self over something that must be simple, access an element created in one xaml page in a different directory within the solution, through the code behind of another xaml page in which it is included using:

<Directory:PageName DockPanel.Dock="Top" Loaded="PageName_Loaded"/>

where Directory is the name of the directory it is in in and PageName is the name of the xaml page it is declared in, in order to access the element created in that xaml page, using its name property so I can perform a method on it from the code behind of the other, main window xaml page.
Ulysses31

You can use google to search for other answers

Custom Search

More Threads

• UI Automation over Remote Desktop
• XBAP Sandbox trust site?
• ListBox (Double)Click Event
• Button state for dynamic generated Button stayed Pressed
• Code animation with binding
• use backgroundworker in wpf
• Loading Image file problem
• BitmapImage is not raising the DownloadCompleted event
• Play QuickTime video file Using WPF
• Use Command Property in CheckBox control