简体   繁体   中英

Fetch a value from a codebehind

I have a variable called path (its value is "/ relative/somefolder/myimg.png " ) in the code behind (xaml.cs) and I want to use this variable to set the value of the image source in XAML (UI). Can anyone tell how to do this?

You could either set the value directly from code-behind (like Joh. already mentioned) or you could have a look at data binding to set it directly from XAML.
Data Binding WPF

In codebehind you can do it like this :

Uri path=new Uri(yourPath,UriKind.RelativeOrAbsolute);
yourImage.Source = new BitmapImage(path);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM