2010-04-27 55 views
0

混合4告訴我,這是不合法的標記和它沒有告訴我爲什麼:Silverlight的 - 綁定到的ImageSource矩形填充

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

我從Twitter的飼料拉取數據,保存到一個ImageSource的,然後將其綁定到ImageBrush(如下所示)以用作矩形的填充。這裏是更多的上下文:

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

我在一個Silverlight用戶控件,這是用於一個Silverlight應用程序內部使用此內部。任何想法可能是什麼問題?

+0

這是由方式一個DataTemplate內。 – 2010-04-27 19:57:47

+0

你可以顯示阿凡達屬性,或至少是簽名? – 2010-06-08 15:16:29

回答

0

綁定不能應用於ImageBrush的ImageSource,或者看起來如此。我有類似的問題,並尋找替代品。

0

您無法綁定到ImageBrush的ImageSource,但是您可以綁定到Shape的Fill屬性。所以下面的工作:

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

隨着一類是這樣的:

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

和後面的代碼是這樣的:

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

在這裏你去: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(); 
     } 
    } 
} 

enter image description here