Binding a grid with two datasources in silverlight

不想你离开。 提交于 2019-12-20 05:48:11

问题


I'm having a problem with how I should bind my datagrid in the best way.

The problem is that i would like to use two itemSources. I have a list with alot of incidents, each incident holds a list of buggs. What I would like to do is bind half a gridview row with some of the fields from the incident, and half of the grid with the corresponding bugs that the incident holds.

How would I be able to do this in the best way?

I have access to the componentOne datagrid, but if the normal grid works it's all good.

But I do would like to have a sorting ability.

Thank you


回答1:


Sounds like you just want to make sure that you don't have AutoGenerateColumns="True".

<sdk:DataGrid AutoGenerateColumns="False">    
        <sdk:DataGrid.Columns>
            <sdk:DataGridTextColumn 
                Header="ItemSource1Property1Name" 
                Width="SizeToHeader"
                Binding="{Binding ItemSource1.Property1}" 
                FontSize="20" />
            <sdk:DataGridTextColumn 
                Header="ItemSource1Property2Name" 
                Width="SizeToHeader"
                Binding="{Binding ItemSource1.Property2}" 
                FontSize="20" />
            <sdk:DataGridTextColumn 
                Header="ItemSource2Property1Name" 
                Width="SizeToCells"
                Binding="{Binding ItemSource2.Property1}" 
                FontSize="20" />
        </sdk:DataGrid.Columns>
        .
        .
        .
        Rest of Code
        .
        .
        .
</sdk:DataGrid>

EDIT

If you choose to use an wrapper for your item source then you will have something like this in your code:

public class ItemWrapper
{
    ObservableCollection<ItemSource1Type> ItemSource1 {get;set;}
    ObservableCollection<ItemSource2Type> ItemSource2 {get;set;}
}

And then in your view model or code behind (where ever you want to set the properties you want to bind to):

public class ViewModel //This may just end up being your code behind depending on how you have your entire SL project set up.
{
    ItemWrapper ItemWrapper { get; set; } //set the two properties either in the constructor or in whatever code initializes ViewModel.
}

In your xaml:

<sdk:DataGrid AutoGenerateColumns="False" 
              ItemSource="{Binding ItemWrapper}"
              DataContext="{Binding}">    
        <sdk:DataGrid.Columns>
            <sdk:DataGridTextColumn 
                Header="ItemSource1Property1Name" 
                Width="SizeToHeader"
                Binding="{Binding ItemSource1.Property1}" 
                FontSize="20" />



回答2:


I'm agree with Kavet's idea but his solution is far away then the reality. According to http://msdn.microsoft.com/en-us/library/system.windows.controls.itemscontrol.itemssource(v=vs.95).aspx

ItemSourse must have IEnumerable interface implemented.

that mean your ItemWrapper will not work at all.

Have you tried to run this?



来源:https://stackoverflow.com/questions/4224747/binding-a-grid-with-two-datasources-in-silverlight

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!