Mark Chassy

Mark Chassy

  • NA
  • 2
  • 15.1k

Binding a Linq Query into a ListView in WPF

Nov 5 2010 8:13 AM
I am looking an example of the following:

I have a WPF window with a listview (or in fact any object would be fine if it works better).
I have a rather complex linq query (I know the query is correct as I have tested in Linqpad).
In the query there a couple of dummy fields which I will have to fill in with a supplementary query.

I would like to:
  1. Bind some elements of the query to the list view
  2. Update the dummy fields in the list view using the supplementary query
  3. Be able to select one line in the list to go to a detail window with the full line from the query and the extra data I updated in the dummy fields.

Thanx for any info.

Mark

To be more specific :

Here is the code on the creation of the Window :


            InitializeComponent();
            _app = _Application;
            _session = _Session;
            _func = new CSNetFunctions.Functions(_session, _app, _dConnect);
            TTNavDB01DataContext _navDB = new TTNavDB01DataContext(_dConnect[Functions._nav]);
            var getStates = from s in _navDB.TP_MT_Scripts
                            join ss in _navDB.MT_ScriptStates on new { s.tpID, s.ComponentTC_ID } equals new { ss.tpID, ss.ComponentTC_ID }
                            join st in _navDB.MT_States on ss.StateID equals st.ID
                            join pr in _navDB.TE_ProductReleases on ss.ProductReleaseID equals pr.ProductReleaseId
                            //where s.TpMVersionID == "19E04398FB82435CB21A80EFEBAD62AF"
                            where s.tpMVersionID == _mvNode.Id.ToString()
                            orderby s.TestCaseID ascending, ss.ID descending
                            select new
                            {
                                s.ComponentTC_ID,
                                s.tpID,
                                s.TestCaseID,
                                ss.ID,
                                st.State,
                                ss.StateDate,
                                ss.StateBy,
                                pr.TeamTrackName,
                                ss.Comments,
                                Locked = true,
                                Bypassed = true,
                                s.ExternalScript,
                                s.EquContract,
                                s.FinContract,
                                s.ScriptType
                            } ;

            var groupStates = from gs in getStates group gs by new { gs.TestCaseID } into gsGroup select new { ls = gsGroup.First() };
            scriptList.DataContext = groupStates;

And here is the xaml code:

Window x:Class="CSNetWindows.statesPerVersion"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="View latest Script states for Version " Height="267" Width="825">
    <Grid>
        <ListView x:Name="scriptList" HorizontalContentAlignment="Stretch">
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="TestCase" Width="50" 
      DisplayMemberBinding="{Binding Path=ls.TestCaseID}" />
                    <GridViewColumn Header="State" Width="50" 
      DisplayMemberBinding="{Binding Path=ls.State}" />
                    <GridViewColumn Header="Date" Width="50" 
      DisplayMemberBinding="{Binding Path=ls.StateDate}" />
                    <GridViewColumn Header="Tester" Width="50" 
      DisplayMemberBinding="{Binding Path=ls.StateBy}" />
                    <GridViewColumn Header="Release" Width="50" 
      DisplayMemberBinding="{Binding Path=ls.TeamTrackName}" />
                    <GridViewColumn Header="Locked" Width="50" 
      DisplayMemberBinding="{Binding Path=ls.Locked}" />
                    <GridViewColumn Header="ByPassed" Width="50" 
      DisplayMemberBinding="{Binding Path=ls.ByPassed}" />
                    <GridViewColumn Header="External Script" Width="50" 
      DisplayMemberBinding="{Binding Path=ls.ExternalScript}" />
                    <GridViewColumn Header="Equ Contract" Width="50" 
      DisplayMemberBinding="{Binding Path=ls.EquContract}" />
                    <GridViewColumn Header="Fin Contract" Width="50" 
      DisplayMemberBinding="{Binding Path=ls.FinContract}" />
                    <GridViewColumn Header="Type" Width="50" 
      DisplayMemberBinding="{Binding Path=ls.FinContract}" />
                    <GridViewColumn Header="Comments" Width="50" 
      DisplayMemberBinding="{Binding Path=ls.ScriptType}" />
                </GridView>
            </ListView.View>
        </ListView>
        
    </Grid>
</Window>