【问题标题】:datatemplate binding image brush sourcedatatemplate 绑定图片刷源
【发布时间】:2013-04-04 14:24:27
【问题描述】:

我正在将 imagebrush 源绑定到我在 xaml 中的数据模板。 数据模板是--->

<DataTemplate x:Key="UserGridListTemplate">
            <Grid Height="140" Width="155">
                <Grid.Background>
                    <ImageBrush ImageSource="{Binding imagePath}"/>
                </Grid.Background>
            </Grid>
</DataTemplate>

和xaml--->

<ListBoxItem ContentTemplate="{StaticResource UserGridListTemplate}" >
      <local:MultiLineItem  ImagePath="/ShareItMobileApp;component/Images/facebook-avatar(1).png"/>
</ListBoxItem>

但发生异常 AG_E_PARSER_BAD_PROPERTY_VALUE [行:3 位置:33]

谁能帮我解决这个问题???

【问题讨论】:

    标签: c# windows-phone-7 xaml datatemplate


    【解决方案1】:

    您收到该错误的原因是因为ImageBrush 不是从FrameworkElement 派生的,这意味着您不能像那样直接绑定数据。您可以创建一个转换器,将您的 imagePath 转换为 ImageBrush 并将该 ImageBrush 设置为网格的 Background 属性的背景。

    首先您需要创建一个转换器来将您的路径字符串转换为 ImageBrush。

    public class ImagePathConverter : IValueConverter
    {     
        public object Convert(object value, Type targetType, object parameter)
        {
            if(value == null) return null;
    
            Uri uri = new Uri(value.ToString(), UriKind.RelativeOrAbsolute);
            ImageBrush ib = new ImageBrush();
            ib.ImageSource = new BitmapImage(uri);
            return ib;
        }
    
        public object ConvertBack(object value, Type targetType, object parameter)
        {
            throw new NotImplementedException();
        }
    }
    

    然后,您可以在 Grid 的后台绑定上使用该转换器(在您将其添加为具有密钥 ImgPathConverter 的资源之后)。

    <DataTemplate x:Key="UserGridListTemplate">
       <Grid Height="140" Width="155"
         Background={Binding imagePath, Converter={StaticResource ImgPathConverter}}/>
    </DataTemplate>
    

    【讨论】:

      猜你喜欢
      • 2014-08-25
      • 2013-01-05
      • 2011-07-21
      • 1970-01-01
      • 2011-08-11
      • 1970-01-01
      • 2014-01-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多