DataBinding是一个二度子类(DataBinding a 2nd degree subclass)
我需要帮助绑定数据,特别是如果根数据源对象有许多子类,并且那些子类也有子类。 这是我的DataModel:
public class NowShowingMovies { public ObservableCollection<Movie> MovieCollection { get; set; } public string Status { get; set; } public string Total{ get; set; } } public class Movie { public string Id {get;set;} public string Title {get;set} public UserRating Rating {get;set;} } public class UserRating { public string UserRatingURL {get;set;} }
我的XAML代码是:
<GridView ItemsSource="{Binding MovieCollection}" <GridView.ItemTemplate> <DataTemplate> <Grid> <Border Background="White"> <TexBlock Text="{Binding Title}"/> </Border> <Grid Background="Black" Margin="0,0,0,0" Opacity="0.75" x:Name="grid_rating"/> <Image Source ="{Binding Path=MovieCollection.Rating.UserRatingURL}" /> </Grid> </DataTemplate> </GridView.ItemTemplate> </GridView>
问题是我无法使其发挥作用。 UserRatingURL未显示。 我甚至改成了
<Image Source ="{Binding Path=MovieCollection[0].Rating.UserRatingURL}" />
而且,没有运气。 我究竟做错了什么? 提前致谢!
I need help in binding data especially if the root datasource object has many subclasses and those subclasses has a subclass too. Here is my DataModel:
public class NowShowingMovies { public ObservableCollection<Movie> MovieCollection { get; set; } public string Status { get; set; } public string Total{ get; set; } } public class Movie { public string Id {get;set;} public string Title {get;set} public UserRating Rating {get;set;} } public class UserRating { public string UserRatingURL {get;set;} }
And my XAML code is:
<GridView ItemsSource="{Binding MovieCollection}" <GridView.ItemTemplate> <DataTemplate> <Grid> <Border Background="White"> <TexBlock Text="{Binding Title}"/> </Border> <Grid Background="Black" Margin="0,0,0,0" Opacity="0.75" x:Name="grid_rating"/> <Image Source ="{Binding Path=MovieCollection.Rating.UserRatingURL}" /> </Grid> </DataTemplate> </GridView.ItemTemplate> </GridView>
Problem is I can't make it work. UserRatingURL is not showing. I even changed it to
<Image Source ="{Binding Path=MovieCollection[0].Rating.UserRatingURL}" />
and still, no luck. What am I doing wrong? Thanks in advance!
原文:https://stackoverflow.com/questions/19238399
更新时间:2024-04-18 16:04
最满意答案
我能够通过使用普通的Linq查询找到解决方案。
config = new MapperConfiguration(cfg => { cfg.CreateMap<User, UserViewModel>().ForMember(m=>m.UserRolesList, opt => opt.MapFrom(source =>source.UserRoles.Where(w=>w.UserId==source.Id).Select(w=>w.Role.Name).ToList())); });
I was able to find a solution by using normal Linq Query.
config = new MapperConfiguration(cfg => { cfg.CreateMap<User, UserViewModel>() .ForMember(m => m.UserRolesList, opt => opt.MapFrom(source => source.UserRoles .Where(w => w.UserId == source.Id) .Select(w => w.Role.Name) .ToList())); });
相关问答
更多-
自动映射器嵌套映射(Automapper nested mapping)[2022-11-19]
它缺少从Address到CompanyInformationDTO的映射,因为这些对象位于相同的嵌套级别。 该地图是为MatchCompanyRequest - > MatchCompanyRequestDTO创建的,但无法确定它是否可以将Address映射到CompanyInformationDTO 。 因此,您的MatchCompanyRequestDTO实际上可能与您的CompanyInformationDTO具有相同的声明: public class MatchCompanyRequestDTO { ... -
我通常会做类似于to_id和from_id的事情,然后使用类似的东西 我想所有你需要的是在这个环节 HTH Sam I usually will do something like having a to_id and a from_id and then use something like this I think all that you need is in that link HTH Sam
-
我能够通过使用普通的Linq查询找到解决方案。 config = new MapperConfiguration(cfg => { cfg.CreateMap
().ForMember(m=>m.UserRolesList, opt => opt.MapFrom(source =>source.UserRoles.Where(w=>w.UserId==source.Id).Select(w=>w. ... -
Hibernate Mapping包含外键的复合主键(Hibernate Mapping composite primary key that contains foreign keys)[2022-07-13]
在RuleMap类上使用MapId注释。 使用@Embeddable和@EmbeddedId也会更好,因为它们更新并且是JPA提供商推荐的。 请参见如何在JPA中创建包含@ManyToOne属性的复合主键作为@EmbeddedId? 以及如何在JPA中映射复合主键,其中主键的一部分是外键 。 Use MapId annotation on your RuleMap class. You will also be better off using @Embeddable and @EmbeddedId sin ... -
你可以这样做是使用.ConstructUsing告诉AutoMapper如何创建对象你可以创建一个手动映射Value1 , Value2 , Value3 ,然后让AutoMapper映射其余属性的函数。 例如: static destination ConstructDestination(source src) { List
chunked = src.Value .Select((ch, index) => new { Character = ch, Index ... -
这不可能。 实体框架只能映射相关实体的集合。 所以你应该使用实体集合而不是密钥集合: public class Driver { public int Id { get; set; } ... public virtual ICollection
Drives { get; set; } } public class Car { public int Id { get; set; } ... public virtual ICollection< ... -
这是你想要实现的目标吗? int_dict = { 0 : "String1", 1 : "String2", 2 : "String3", 16 : "String5" }; #range is first inclusive last exlusif, watch out for that for i in range(3 ...
-
显然, Work可能包含零个或多个Document1 ,零个或多个Document2以及零个或多个Document3 。 所有这些文件都是Document类型。 每个文档只属于一个Work ,它是Document1 , Document2或Document3 。 问题是当使用您的类时,它不知道它是哪种Document类型。 最友好和最灵活的解决方案是为每个文档提供一个属性,告诉它是哪种类型的文档,并将所有文档放在一个表中: public enum DocumentType { Document1, ...
-
用于在Python或Ruby中自动映射外键的ORM库(ORM library for automatically mapping foreign keys in Python or Ruby)[2022-02-25]
您应该查看SQLAlchemy中的SQLSoup扩展 。 它声称为你做这一切(包括外键)。 我自己没有测试过。 You should take a look at the SQLSoup extension in SQLAlchemy. It claims to do all this for you (including foreign keys). I haven't tested it myself. -
我相信是这样。 只需将它们连接成一个字符串并返回它。 var mapping = { key: function(data) { return ko.unwrap(data.id) + '_' + ko.unwrap(data.name); } } 下划线定界符(或管道或您选择使用的任何内容)应该有助于任何关键冲突,例如id ='12'name ='times'的另一个对象,另一个id ='1'且name ='2times'。 I believe so. Just c ...