Silverlight - Reliure ImageSource à Rectangle Fill
-
02-10-2019 - |
Question
Blend 4 me dit que c'est une balise invalide et sa ne me dit pas pourquoi:
<ImageBrush Stretch="Fill" ImageSource="{Binding Avatar, Mode=OneWay}"/>
Je tire des données d'un flux Twitter, sauver un ImageSource et liant ensuite à un ImageBrush (comme on le voit ci-dessous) pour être utilisé comme remplissage pour un rectangle. Voici plus de contexte:
<Rectangle x:Name="Avatar" RadiusY="9" RadiusX="9" Width="45" Height="45" VerticalAlignment="Center" HorizontalAlignment="Center" >
<Rectangle.Fill>
<ImageBrush Stretch="Fill" ImageSource="{Binding Avatar, Mode=OneWay}"/>
</Rectangle.Fill>
</Rectangle>
J'utilise ceci à l'intérieur d'un Silverlight UserControl, qui est utilisé à l'intérieur d'une application Silverlight. Toutes les idées sur ce que pourrait être le problème?
La solution
La liaison ne peut pas être appliquée à la ImageSource d'un ImageBrush, ou paraît-il. Je vais avoir un problème similaire et la recherche d'alternatives.
Autres conseils
Vous ne pouvez pas lier à la ImageSource d'un ImageBrush, vous pouvez cependant se lier à la propriété de remplissage d'une forme. Ainsi, les travaux suivants:
<Rectangle Name="myRect" Fill="{Binding Avatar}"/>
Avec quelque chose de classe comme ceci:
public class AvatarClass
{
public ImageBrush Avatar { get; set; }
}
et le code derrière comme ceci:
myRect.DataContext = new AvatarClass{
Avatar = new ImageBrush {
ImageSource = new BitmapImage(avatarUri)}};
Ici, vous allez: 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>
Blockquote
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";
}
}
}
Blockquote Blockquote
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();
}
}
}