1

I'm working on a C# metro (store) app which has a UI similar to the following code (as an example) :

<Canvas x:Name="canvas">
<Image x:Name="image" Canvas.Left="600" Canvas.Top="170"/>
</Canvas>

The containing canvas has a function "AddToLog(string text)" and I want this function to be accessible by the objects inside the canvas (i.e. here "image") also. The image may also be a User Control contained in a separate *.xaml file. Is this possible, and if so how would I go about doing it ? If not, are there any alternate ways to do this ?

user2864740
  • 60,010
  • 15
  • 145
  • 220
Arctic Vowel
  • 1,492
  • 1
  • 22
  • 34

1 Answers1

1

You can easily get containing Canvas from Image's Parent property :

var canvas = (Canvas)image.Parent;
//at this point you're able to access any function defined in Canvas
//canvas.AddToLog("something");
har07
  • 88,338
  • 12
  • 84
  • 137
  • How can I access a function defined in the class `MainPage : Page` from a function in a separate `UserControl.xaml` file ? – Arctic Vowel Aug 30 '14 at 10:50
  • 1
    if you meant in the same situation (where `MainPage` is parent of `UserControl`) then you can still use `Parent` property... – har07 Aug 30 '14 at 10:53