rism
rism

Reputation: 12132

Programmatically set WinRt button content to image

For a given button control in Winrt I want to dynamically set it's content to an image. I would have thought this would be dead simple:

 _subjectFilePoster = new BitmapImage();
 _subjectFilePoster.SetSource(t);
 _btnPlayVideo.Content = _subjectFilePoster;

But it's not, instead I get a button with the typename Windows.Ui.Xaml.Media.Imaging.BitmapImage written on it.

Upvotes: 1

Views: 334

Answers (2)

Chubosaurus Software
Chubosaurus Software

Reputation: 8161

Need to set its Content to <Image> and the <Image>'s Source to the BitmapImage

_subjectFilePoster = new BitmapImage();
_subjectFilePoster.SetSource(t);

Image i = new Image();
i.Source = _subjectFilePoster;

_btnPlayVideo.Content = i;

Upvotes: 1

Jerry Nixon
Jerry Nixon

Reputation: 31813

Here's how I do it:

<Button x:Name="MyButton">
    <Image Source="{Binding}" />
</Button>

With this:

this.MyButton.DataContext = "http://server/image.png";

Why does it work? Because you get to use the native type converter in the underlying XAML framework. It's the easiest way to do it. It certainly works. I use this technique all the time.

You can do it like this:

<Button>
    <Image x:Name="MyImage" Source="{Binding}" />
</Button>

Then with this:

this.MyImage.DataContext = "http://server/image.png";

Is it the only way? No. Is it the easiest way? Yes.

The best of both words: binding and code-behind.

Best of luck!

Upvotes: 0

Related Questions