문제

I have in a UserControl the Property ImageSource. How can I set the ImageSource in my code behind to a Image in my Resources directory?

I want to bind the Image Source to the property ImageSource.

<Image Source="{Binding Path=ImageSource}" />
도움이 되었습니까?

해결책

In order to create a BitmapImage (which is derived from ImageSource) from a resource file in code, you would do the following, provided that the file MyImage.jpg is in a folder named Images of your Visual Studio project, and that its Build Action is set to Resource:

var uri = new Uri("pack://application:,,,/Images/MyImage.jpg"); 
ImageSource = new BitmapImage(uri); // set the ImageSource property

See also this answer.


You could also directly use the image resource without binding:

<Image Source="/Images/MyImage.jpg" />
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top