简体   繁体   English

如何在后面的代码中设置ViewBox图像源

[英]How to Set ViewBox Image Source in Code Behind

In following a code sample here http://msdn.microsoft.com/en-us/library/ms752301(v=vs.110).aspx I'd like to be able to change the following XAML code to C# and set the ViewBox source in code behind. 在下面的代码示例中, http://msdn.microsoft.com/zh-cn/library/ms752301(v = vs.110).aspx我希望能够将以下XAML代码更改为C#并设置ViewBox源代码在后面。 What is the proper method of doing this? 这样做的正确方法是什么? Must I set the Child of the ViewBox? 我必须设置ViewBox的子级吗?

<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
    <Viewbox MaxWidth="500" MaxHeight="500" Name="vb1">
        <Image Source="tulip_farm.jpg"/>
    </Viewbox>    
</StackPanel>

The easiest way to achieve this is give a name to that specific image so you can set the source anywhere you want in code: 实现此目的的最简单方法是为该特定图像命名,以便您可以在代码中的任何位置设置源:

         <Image x:Name="MyImage" Source="tulip_farm.jpg"/>

And in c#: 在C#中:

        BitmapImage bitimg = new BitmapImage(new Uri(@"/MyNewLink.jpg", UriKind.Relative));
        MyImage.Source = bitimg as ImageSource;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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