WP7中Button的Image背景图片路径获取

<Button  Grid.Row="1" Height="201" HorizontalAlignment="Left" Margin="198,13,0,0" Name="button3"      
                        VerticalAlignment="Top" Width="215" Grid.ColumnSpan="2"  BorderThickness="0" Click="button3_Click">
                    <Button.Background>
                        <ImageBrush ImageSource="/MyHometown;component/photos/4.jpg" Stretch="Fill" />
                    </Button.Background>
                </Button>


获得背景图片地址,然后在其他页面使用。

private void button1_Click(object sender, RoutedEventArgs e)
        {
            ImageBrush i = button1.Background as ImageBrush; //background属性是Brush类 转换成ImageBrush因为图片的ImageSource是ImageBrush类。。。
        BitmapImage ii = i.ImageSource as BitmapImage; // BitmapImage类中有UriSource属性,该属性就是图片的相对地址,而ImageBrush的ImageSource属性是父类是BitmapImage,转换一下就OK
            txtText.Text = ii.UriSource.ToString(); //得到图片地址,或保存在变量中
        }


原问题链接:http://bbs.itheima.com/forum.php?mod=viewthread&tid=7846&highlight=

posted @ 2012-04-05 23:23  AGA2012  阅读(625)  评论(0编辑  收藏  举报