Silverlight - ImageSource rilegatura a rettangolo Fill
-
02-10-2019 - |
Domanda
Blend 4 mi sta dicendo questo è markup valido e la sua non dirmi perché:
<ImageBrush Stretch="Fill" ImageSource="{Binding Avatar, Mode=OneWay}"/>
sto tirando i dati da un feed Twitter, il risparmio a un ImageSource, e quindi vincolante a un ImageBrush (come mostrato qui sotto) per essere utilizzato come riempimento per un rettangolo. Qui è più contesto:
<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>
sto usando questo all'interno di un Silverlight UserControl, che viene utilizzato all'interno di un'applicazione Silverlight. Tutte le idee su ciò che il problema potrebbe essere?
Soluzione
vincolante non può essere applicato a ImageSource di un ImageBrush, o almeno così sembra. Sto avendo un problema simile e alla ricerca di alternative.
Altri suggerimenti
Non è possibile associare al ImageSource di un ImageBrush, tuttavia è possibile associare alla proprietà Fill di una forma. Quindi, le seguenti opere:
<Rectangle Name="myRect" Fill="{Binding Avatar}"/>
Con una classe simile a questa:
public class AvatarClass
{
public ImageBrush Avatar { get; set; }
}
e il codice dietro in questo modo:
myRect.DataContext = new AvatarClass{
Avatar = new ImageBrush {
ImageSource = new BitmapImage(avatarUri)}};
Qui si va: 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();
}
}
}