どうぞ:WPF/Silverlight
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:convertor="clr-namespace:WpfApplication1.Converters"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<convertor:RectangleImageFillConvertor x:Key="RectangleImageFillConvertor" />
</Window.Resources>
<Grid>
<Rectangle HorizontalAlignment="Center"
RadiusX="10"
RadiusY="10"
Width="200"
Height="200"
Fill="{Binding ImageUrl, Converter={StaticResource RectangleImageFillConvertor}}"/>
</Grid>
</Window>
引用符
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public string ImageUrl { get; set; }
public MainWindow()
{
InitializeComponent();
this.DataContext = this;
ImageUrl = "http://www.deargrumpycat.com/wp-content/uploads/2013/02/Grumpy-Cat1.jpg";
}
}
}
ブロッククオート ブロッククオート
namespace WpfApplication1.Converters
{
public class RectangleImageFillConvertor : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
try
{
return new ImageBrush(new BitmapImage(new Uri(value as string, UriKind.RelativeOrAbsolute)));
//if silverlight
// return new ImageBrush{ ImageSource = new BitmapImage(new Uri(value as string, UriKind.RelativeOrAbsolute))};
}
catch
{
return null;
}
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}