开发者

Autogenerating columns from empty results in Silverlight

I'm doing some rapid prototyping and is try开发者_StackOverflowing to mock out an admin interface for a website and went with WCF RIA Services. I'm able to expose and consume the domain services from server to client, but I'm struggling with getting columns autogenerated in the datagrid when the result of the query on the server holds no data.

 <riaControls:DomainDataSource Name="domainDataSource1" 
        LoadSize="20" QueryName="GetUsers" AutoLoad="True" >
            <riaControls:DomainDataSource.DomainContext>
                <ds:CobraDomainContext />
            </riaControls:DomainDataSource.DomainContext>
</riaControls:DomainDataSource>

<sdk:DataGrid ItemsSource="{Binding Data, ElementName=domainDataSource1}" AutoGenerateColumns="True" IsReadOnly="False" Width="250" Height="150" >
            </sdk:DataGrid>

This renders an empty 250x150 datagrid (no columns/no rows). I was expecting that the colunms for the user entity were displayed even though no data was returned from the server since the view would kind of suck otherwise initially. My brain can't can't seem to figure out what is wrong, so I'll crowdsource with stackoverflow.

UPDATE: I was kind of expecting the result from the query to be a typed enumeration, but it appears that the result of the query on the DomainDataService is just IEnumerable but not typed, so the internal logic needs to look into the list to discover what kind of data it is containing.

So the updated question is: Can I give the DataGrid a hint on what type of data will be returned or otherwise autogenerate columns in the grid (through XAML or code)??


I guess your ItemsSource enumeration is not typed, right? If it's just a list of objects, the datagrid will not find the entity public properties.

I don't know how your entity class is, but try this code to see what I'm talking about:

Somewhere:

public class User {
    public string Name { get; set; }
    public int Age { get; set; }
}

Xaml:

 <my:DataGrid x:Name="datagrid"/>

Codebehind:

public MainPage() {
    InitializeComponent();
    datagrid.ItemsSource = new List<User>();
}

The list is empty but datagrid will pick the column names since I'm using a List of Users. It could be an Enumeration of Users, etc..

I hope it helps,

Cheers

EDITED: about the updated question: You could try using a Converter.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜