【问题标题】:Silverlight - Binding ImageSource to Rectangle FillSilverlight - 将 ImageSource 绑定到矩形填充
【发布时间】:2010-04-27 19:11:12
【问题描述】:

Blend 4 告诉我这是无效标记,它没有告诉我原因:

<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 应用程序中使用。关于问题可能是什么的任何想法?

【问题讨论】:

  • 顺便说一句,这是在 DataTemplate 中。
  • 你能显示 Avatar 属性,或者至少是签名吗?

标签: silverlight binding imagebrush


【解决方案1】:

绑定不能应用于 ImageBrush 的 ImageSource,或者看起来是这样。我遇到了类似的问题,正在寻找替代方案。

【讨论】:

    【解决方案2】:

    您不能绑定到 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)}};
    

    【讨论】:

      【解决方案3】:

      给你: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();
              }
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-04-27
        • 2012-08-27
        • 2011-09-14
        • 2012-02-02
        • 1970-01-01
        • 1970-01-01
        • 2019-07-19
        • 2019-03-01
        相关资源
        最近更新 更多