将Gridview itemsource绑定到viewmodel属性

时间:2016-05-12 17:04:24

标签: c# mvvm windows-10-universal

我在将viewmodel中的collection属性绑定到GridView时遇到了一些问题。我正在使用MVVM灯,我相信我正确设置了ViewModelLocator,并在页面的xaml中设置了DataContext。

模型

public class Base
{
    public ObservableCollection<Downloads> results { get; set; }
}
public class Downloads
{
    public int id { get; set; }
    public string name { get; set; }
    public int trackNumber { get; set; }
    public string mixName { get; set; }
    public string title { get; set; }
    public string slug { get; set; }
    public string releaseDate { get; set; }
    public string publishDate { get; set; }
    public List<Artist> artists { get; set; }
    public string artistNames
    {
        get
        {
            return (artists == null)
                ? string.Empty
                : string.Join(", ", artists.Select(a => a.name));
        }
    }
    public string artistNamesSlug
    {
        get
        {
            return (artists == null)
                ? string.Empty
                : string.Join("_", artists.Select(a => a.name));
        }
    }

    public Release release { get; set; }
    public Label label { get; set; }
    public Image images { get; set; }
    public int downloadId { get; set; }
    public string audioFormat { get; set; }
    public string downloadUrl { get; set; }
}
public class Release
{
    public int id { get; set; }
    public string name { get; set; }
    public string type { get; set; }
    public string slug { get; set; }
}
public class Label
{
    public int id { get; set; }
    public string name { get; set; }
    public string type { get; set; }
    public string slug { get; set; }
    public bool status { get; set; }
}
public class Image
{
    public LargeImage large { get; set; }
}
public class LargeImage
{
    public int id { get; set; }
    public int width { get; set; }
    public int height { get; set; }
    public string url { get; set; }
    public string secureUrl { get; set; }
}

视图模型

public class AvailableViewModel : ViewModelBase
{

    public AvailableViewModel()
    {

    }

    private Base availableDownloads;
    public Base AvailableDownloads
    {
        get
        {
            if(availableDownloads == null)
            {
                GetData();
            }
            return availableDownloads;
        }
        set
        {
            availableDownloads = value;
            RaisePropertyChanged(() => AvailableDownloads);
        }
    }

    private async void GetData()
    {
        OAuth oauth = new OAuth();

        string httpMethod = "GET";
        string parameters = "status=available";
        string response = await oauth.GetData(OAuth.availDownloadsUrl, httpMethod, parameters);

        Base availableDownloads = JsonConvert.DeserializeObject<Base>(response);
    }
}

XAML

DataContext="{Binding Available, Source={StaticResource Locator}}">

<Page.Resources>
    <DataTemplate x:Key="AvailableGridView">
        <Grid Margin="0,10,0,0">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="2*"/>
            </Grid.ColumnDefinitions>
            <Image Source="{Binding AvailableDownloads.images.large.url}" Grid.Column="0" />
            <StackPanel Grid.Column="1" VerticalAlignment="Top" Margin="20,0,0,0">
                <TextBlock Text="{Binding AvailableDownloads.title}" Style="{StaticResource BaseTextBlockStyle}" TextWrapping="Wrap"/>
                <TextBlock Text="{Binding AvailableDownloads.release.name}" Style="{StaticResource BaseTextBlockStyle}"/>
                <TextBlock Text="{Binding AvailableDownloads.artistNames}" Style="{StaticResource SubtitleTextBlockStyle}"/>
            </StackPanel>
        </Grid>
    </DataTemplate>
</Page.Resources>


<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <GridView ItemsSource="{Binding AvailableDownloads.results}" SelectionMode="Multiple" ItemTemplate="{StaticResource AvailableGridView}"/>
</Grid>

这可能是问题的一部分,但是当我在xaml中设置DataContext时,布局会显示一个Object Reference错误。我不知道为什么会这样,但应用程序将编译并运行。我是MVVM的新手,我似乎无法弄清楚为什么我的绑定在这里不起作用。

1 个答案:

答案 0 :(得分:0)

  

这可能是问题的一部分,但是当我在xaml中设置DataContext时,布局会显示一个Object Reference错误。

这是“对象引用错误”NullReferenceException错误吗?由于你的代码不全面,我可以在几个地方重现这个问题,但我不知道究竟是什么导致了你的问题。

首先我在这里更改了你的XAML代码以进行测试,我在DataTemplate的{​​{1}}使用了x:Bind:

GridView

我改变了你的 ... DataContext="{Binding Available, Source={StaticResource Locator}}" xmlns:model="using:[Namespace of your app].Model"> <Page.Resources> <DataTemplate x:Key="AvailableGridView" x:DataType="model:Downloads"> <Grid Margin="0,10,0,0"> <Grid.ColumnDefinitions> <ColumnDefinition Width="*" /> <ColumnDefinition Width="2*" /> </Grid.ColumnDefinitions> <!--<Image Source="{x:Bind downloadUrl}" Grid.Column="0" />--> <StackPanel Grid.Column="1" VerticalAlignment="Top" Margin="20,0,0,0"> <TextBlock Text="{x:Bind title}" Style="{StaticResource BaseTextBlockStyle}" TextWrapping="Wrap" /> <TextBlock Text="{x:Bind release.name}" Style="{StaticResource BaseTextBlockStyle}" /> <TextBlock Text="{x:Bind artistNames}" Style="{StaticResource SubtitleTextBlockStyle}" /> </StackPanel> </Grid> </DataTemplate> </Page.Resources> <Grid> <GridView ItemsSource="{Binding AvailableDownloads.results}" SelectionMode="Multiple" ItemTemplate="{StaticResource AvailableGridView}" /> </Grid> 课程:

Base

如果您在添加数据时未创建public class Base { public ObservableCollection<Downloads> results { get; set; } public Base() { results = new ObservableCollection<Downloads>(); } } 的新实例,则可能是这种情况。

我改变了你的ObservableCollection<Downloads>,添加到AvailableViewModel()的数据是假的,只是为了测试:

result

如您所见,我在这里创建了一个public AvailableViewModel() { availableDownloads = new Base(); availableDownloads.results.Add(new Downloads { title = "11111", artistNames = "222", release = new Release(0, "333", "", "") }); availableDownloads.results.Add(new Downloads { title = "11111" }); availableDownloads.results.Add(new Downloads { title = "11111" }); availableDownloads.results.Add(new Downloads { title = "11111" }); availableDownloads.results.Add(new Downloads { title = "11111" }); } 类的新实例。

我注意到您在xaml代码中使用了Base,为此,我认为您需要像这样修改release.name类:

Release

您可以像这样创建public class Release { public Release() { } public Release(int Id, string Name, string Type, string Slug) { this.id = Id; this.name = Name; this.type = Type; this.slug = Slug; } public int id { get; set; } public string name { get; set; } public string type { get; set; } public string slug { get; set; } } 类的实例:

Release

我评论了release = new Release(0, "333", "", ""); 中的Image控件,为了完成这项工作,您需要像DataTemplate类一样修改LargeImageImage。< / p>