Question

Blend 4 is telling me this is invalid markup and its not telling me why:

<ImageBrush Stretch="Fill"  ImageSource="{Binding Avatar, Mode=OneWay}"/>

I'm pulling data from a Twitter feed, saving to an ImageSource, and then binding it to an ImageBrush(as seen below) to be used as the Fill for a Rectangle. Here is more context:

<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>

I'm using this inside of a Silverlight UserControl, which is used inside of a Silverlight Application. Any Ideas on what the problem could be?

Was it helpful?

Solution

Binding cannot be applied to an ImageBrush's ImageSource, or so it seems. I'm having a similar issue and looking for alternatives.

OTHER TIPS

You can't bind to the ImageSource of an ImageBrush, however you can bind to the Fill property of a Shape. So the following works:

<Rectangle Name="myRect" Fill="{Binding Avatar}"/>

With a class something like this:

public class AvatarClass
{
    public ImageBrush Avatar { get; set; }
}

and code behind like this:

 myRect.DataContext = new AvatarClass{ 
                       Avatar = new ImageBrush {
                        ImageSource = new BitmapImage(avatarUri)}};

Here you go : 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();
        }
    }
}

enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top