简体   繁体   English

MVVM XAML绑定转换器无法转换值错误

[英]Mvvm xaml binding converter failed to convert value error

I'm trying to bind in a Listview to a ObservableCollection. 我正在尝试将Listview绑定到ObservableCollection。 I get the following error. 我收到以下错误。

Debugger Message: 调试器消息:

Error: Converter failed to convert value of type 'System.Collections.ObjectModel.ObservableCollection`1' to type 'ImageSource'; 错误:转换器无法将类型'System.Collections.ObjectModel.ObservableCollection`1'的值转换为类型'ImageSource'; BindingExpression: Path='PictureSource' DataItem='MeetFriends.MainViewModel.View'; BindingExpression:Path ='PictureSource'DataItem ='MeetFriends.MainViewModel.View'; target element is 'Windows.UI.Xaml.Controls.Image' (Name='null'); 目标元素是“ Windows.UI.Xaml.Controls.Image”(名称=“空”); target property is 'Source' (type 'ImageSource'). 目标属性为“源”(类型为“图像源”)。

My ListView: 我的ListView:

 <ListView ItemsSource="{Binding}" DataContext="{Binding FriendsData, Source={StaticResource view}}" Margin="0,0,2,0" x:Name="ListUI" Loaded="ListUI_Loaded" Background="WhiteSmoke" 
               ScrollViewer.HorizontalScrollBarVisibility="Visible" ScrollViewer.HorizontalScrollMode="Enabled" >
            <ListView.Header>
                <Grid Margin="14,0,0,14" >
                    <TextBlock Foreground="Black" TextWrapping="WrapWholeWords">People you are friends with and using MeetFriends:</TextBlock>
                </Grid>
            </ListView.Header>
            <ListView.ItemTemplate>
                <DataTemplate>
                    <Grid HorizontalAlignment="Stretch" Margin="0,14,0,0" >
                        <StackPanel Orientation="Horizontal" >
                            <TextBlock Text="{Binding name}" Margin="0,0,14,0" FontSize="{Binding Fontsize, Source={StaticResource view}}" FontFamily="Segoe UI Black"></TextBlock>
                            <Image Source="{Binding PictureSource, Source={StaticResource view}}" Height="100" Width="100">
                            </Image>
                        </StackPanel>
                    </Grid>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

And the Binding is to: 绑定是:

   public ObservableCollection<ImageSource> PictureSource
    {
        get { return _pictureSource; }
        set { _pictureSource = value; OnPropertyChanged("_pictureSource"); }
    }

Adding Values is looking like this in the code: 在代码中添加值看起来像这样:

  foreach(object x in f.data) {
                view.FriendsData.Add(f.data[i]);
                view.PictureSource.Add(new BitmapImage() { UriSource = new Uri("https://graph.facebook.com/v2.5/" + view.FriendsData[i].id.ToString() + "/picture?redirect=true") });
                Debug.WriteLine("A new object has been added to friendsdata and picturesource ["+i+"]");
                i++;
            }

Error is pretty self explanatory, you should bind your image to a field of data context of the current item, which is item of FriendsData collection. 错误是不言而喻的,您应该将图像绑定到当前项目(FriendsData集合的项目)的数据上下文字段。

You may bind your image directly to id and use a converter, that will convert that string to an ImageSource: 您可以将图像直接绑定到id并使用转换器,该转换器会将字符串转换为ImageSource:

<Image Source="{Binding id, Converter={StaticResource idToImageConverter}}" Height="100" Width="100" />

Converter: 转换器:

public class IdToImageConverter: IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return new BitmapImage() { UriSource = new Uri("https://graph.facebook.com/v2.5/" + value.ToString() + "/picture?redirect=true") }
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return null;
    }
}

More information on value converters 有关价值转换器的更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM