Silverlight - 将 ImageSource 绑定到矩形填充

发布于 2024-08-30 10:59:32 字数 638 浏览 7 评论 0原文

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 UserControl 内部使用它,该控件在 Silverlight 应用程序内部使用。关于问题可能是什么的任何想法吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

我一直都在从未离去 2024-09-06 10:59:32

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

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

水中月 2024-09-06 10:59:32

您无法绑定到 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)}};

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)}};
耀眼的星火 2024-09-06 10:59:32

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

在此处输入图像描述

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文