开发者

Silverlight control to group other controls together for data binding

Is there a control in silverlight to group controls together for data binding. For instance, say I have a开发者_运维问答 Person object and I want to display fname, lname, age, height, etc. in TextBlocks. Is there a control I can use to group these TextBlock controls together and set the ItemSource on just that control similar to how you set the ItemSource on a DataGrid and then bind each column?


Group the TextBlocks in any layout control and bind the control's DataContext to Person. If not explicitly set, each TextBlock's context will be relative to the parent.

<UserControl DataContext="">
    <UserControl.DataContext>
        <SomeViewModel />
    </UserControl.DataContext>
    <Grid DataContext="{Binding ThePerson}">
        <TextBlock Text="{Binding fname}"/>
        <TextBlock Text="{Binding lname}"/>
        <TextBlock Text="{Binding age}"/>
        <TextBlock Text="{Binding height}"/>
    </Grid>
</UserControl>

View model class...

public class SomeViewModel
{
    public Person ThePerson { get;set; }
}


You may want to read this article on MVVM http://msdn.microsoft.com/en-us/magazine/dd419663.aspx

It sounds like you need to create a "PersonView" and a "PersonViewModel" (and probably a "PersonCollectionViewModel") This would allow you to bind the controls on your "PersonView" (A Silverlight UserControl) to the "PersonViewModel".


You can use DataForm control and set its "IsReadOnly" property to True ,this control usually used to edit data, and remember to set AutoGenerateFields="True" ...

PS : I'm supposing you're using Ria service with silverlight


I think any of these answers is correct, however I don't think will work/are appropriate for my app. The MVVM approach is overkill for my app and the other two approaches will not work for me for various reasons. I will just do everything in the code-behind.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜