luddej
luddej

Reputation: 47

Access a variable on another page UWP

I have this Imagebox in my UserControl.Xaml

 <Image x:Name="MainImage" Margin="20,20,20,20" Source="{Binding FrontImage}" Stretch="Fill" Canvas.ZIndex="2" Width="391" Height="355">

Is there anyway to reach the

 x:Name="MainImage"

in my Mainpage.XAML.cs file?

i have tried this but that didn't seem to work

     public Image MainImage
        {
            get { return MainImage; }
        }

Upvotes: 1

Views: 703

Answers (2)

Yury Schkatula
Yury Schkatula

Reputation: 5369

The short answer is "you shouldn't".

The long answer is: like his predecessor named WPF, UWP framework intends MVVM approach. This means you should avoid placing much logic inside your views (aka "code behind" *.xaml.cs files). Instead, you should work with models and view-models, depending on your very goals.

For instance, at your case you should work with FrontImage (that could be in appropriate view-model) instead of trying to reach MainImage XAML control (that is in your view indeed). Then, you can reach given instance of the view-model via IoC framework you use (or its hand-written equivalent that to resolve such dependencies between view-models and models and to manage created instances in case they're multiple).

Upvotes: 2

Rrr
Rrr

Reputation: 304

I am pretty sure you want something like this:

How can I access a control in WPF from another class or window

Basically you make the xaml property public, then you find the window instances of the application, find the one you want and then modify it.

I cant add comments with my current points, so i leave the link as an answer here. Dont forget to upvote the actual author of the answer.

Upvotes: 1

Related Questions