Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Sharing my Linq/WPF accomplishments...
Message
From
24/06/2008 17:40:44
 
 
To
All
General information
Forum:
ASP.NET
Category:
LINQ
Title:
Sharing my Linq/WPF accomplishments...
Miscellaneous
Thread ID:
01326516
Message ID:
01326516
Views:
60
Just sharing some small accomplishments I've had this week with LINQ and WPF.

Screenshot here.. http://www.jordanmachine.com/wpf_screenshot_2008_06_24.jpg
(The UI is still very rough. I am mostly working on app structure at this time)

There's a WPF ListView control that is populated with Customer objects of customers who have ActiveJobs at this time. Clicking on a Customer row will fire the SelectionChanged event to populate a ListView-in-GridView-mode with all ActiveJobs ojects for that customer. Clicking on an ActiveJobs grid row, will populate a ContentControl with more detail about the selected ActiveJob, and also populate another ListView-in-GridView-mode with all the OrderItem objects of line items that are on the selected ActiveJob.


This method code (called by the ListView SelectionChanged event) shows 3 ways to determine which customer was picked in the ListView.

Method 1 and 2 required setting SelectedValuePath="custno" on the ListView to indicate which field value is returned via the SelectedValue property.

Method 3 lets (requires) you to tell it which field is the key.

(So, take your pick... Tell it in XAML or tell it in the code-behind)
       private void GetJobs(object sender, SelectionChangedEventArgs e)
        {
            ///1.  Here's one way, by hard-coding the name of the ListView name and its settings ---
            //var SelectedCustomerItem = lvCustomerList.SelectedItem;
            //string SelectedCustomerID = lvCustomerList.SelectedValue.ToString();

            //2. John Fenton's way. Requires proper object types be in place on the collection 
            //var SelectedCustomerItem=(customer_source)e.AddedItems[0];
            //string SelectedCustomerID=SelectedCustomerItem.custno;


            //3. Here's another way, assuming a ListView made the call, but without knowing it's name
            //ListView lvSender = (ListView) sender;
            //string SelectedCustomerID = lvSender.SelectedValue.ToString();

            if (SelectedCustomerID != null)
            {
                IEnumerable<job_info> JobRecords = new job_info().GetActiveJobsByCustID(SelectedCustomerID);
                lvJobList.ItemsSource = JobRecords;
                DetailView.Content = JobRecords;
            }
             
        }
Here's the code to get the CustomerList of customers with ActiveJobs. Kind of a hack, anybody know a better way? It must be able to be cast to type (customer_source).
     private void GetCustomerList()
        {
            DataClasses1DataContext db = new DataClasses1DataContext();

            var subq = (from p in db.job_info
                        join c in db.customers on p.cust_num equals c.custno
                        where p.status == 'A'
                        orderby c.company
                        select c.custno).Distinct();

            IQueryable<customer_source> CustomerList = from a in db.customers
                                                       where subq.Contains(a.custno)
                                                       select a;

            //Populate the ListView 
            lvCustomerList.ItemsSource = CustomerList;
Then, when a Customer is selected, it calls a custom method on the Job_Info class to GetActiveJobsByCustID.
Here is that class code:
   public partial class job_info
    {
        public IEnumerable<job_info> GetActiveJobsByCustID(string CustID)
        {
            DataClasses1DataContext db = new DataClasses1DataContext();
            if (CustID != null)
            {
                IEnumerable<job_info> JobRecords = from a in db.job_info
                                                   where a.cust_num == CustID && a.status == 'A'
                                                   orderby a.priority, a.job_num
                                                   select a;
                return JobRecords;
            }
            else
            {
                return null;
            }
        }
    }
And the code to get the JobItems detail records:
        private void GetJobItems(object sender, SelectionChangedEventArgs e)
        {
            ListView lvSender = (ListView)sender;
            string SelectedJobNum = lvSender.SelectedValue.ToString();

            if (SelectedJobNum != null)
            {
                IEnumerable<job_item> JobItems = new job_item().GetJobItemsByJobNo(SelectedJobNum);
                lvJobItems.ItemsSource = JobItems;
            }
        }
And lastly, here's the XAML:
<Window x:Class="wpf2.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="600" Width="800">

    <Window.Resources>
        <DataTemplate x:Key="DetailTemplate">
            <Border Width="300" Height="100" Margin="20" BorderBrush="Aqua" BorderThickness="1" Padding="8">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition/>
                        <RowDefinition/>
                        <RowDefinition/>
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition/>
                        <ColumnDefinition/>
                    </Grid.ColumnDefinitions>
                      <TextBlock Grid.Row="0" Grid.Column="0" Text="Job No:"/>
                      <TextBlock Grid.Row="0" Grid.Column="1" Text="{Binding Path=job_num}"/>
                      <TextBlock Grid.Row="1" Grid.Column="0" Text="PO No:"/>
                      <TextBlock Grid.Row="1" Grid.Column="1" Text="{Binding Path=p_o_num}"/>
                      <TextBlock Grid.Row="2" Grid.Column="0" Text="Status:"/>
                      <TextBlock Grid.Row="2" Grid.Column="1" Text="{Binding Path=status}"/>
    </Grid>
            </Border>
        </DataTemplate>
</Window.Resources>


    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"></RowDefinition>
            <RowDefinition Height="*"></RowDefinition>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="275"></ColumnDefinition>
            <ColumnDefinition Width="*"></ColumnDefinition>
        </Grid.ColumnDefinitions>
        <StackPanel Orientation="Horizontal" Grid.Row="0">
            <TextBox x:Name="txtCustomerFilter" CharacterCasing="Upper" Width="100" Margin="5" KeyUp="ButtonSearch_Click"></TextBox>
            <Button x:Name="ButtonSearch" Width="50" Margin="5" Click="ButtonSearch_Click">Search</Button>
        </StackPanel>
        
        <ListView Name="lvCustomerList" HorizontalContentAlignment="Stretch" ScrollViewer.VerticalScrollBarVisibility="Visible"
                  Grid.Row="1" Background="DarkGray"
                  SelectedValuePath="custno" SelectionChanged="GetJobs">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <Border Margin="5" BorderThickness="1" BorderBrush="SlateGray" CornerRadius="4" Background="CadetBlue">
                        <Grid Margin="3">
                            <Grid.RowDefinitions>
                                <RowDefinition></RowDefinition>
                                <RowDefinition></RowDefinition>
                                <RowDefinition></RowDefinition>
                                <RowDefinition></RowDefinition>
                            </Grid.RowDefinitions>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition></ColumnDefinition>
                            </Grid.ColumnDefinitions>
                            <StackPanel Orientation="Horizontal">
                             <TextBlock FontWeight="Bold" Text="{Binding Path=custno}"></TextBlock>
                             <TextBlock Text="{Binding Path=company}" Margin="5,0,0,0"></TextBlock>
                            </StackPanel>
                            <!--<StackPanel Grid.Row="1" Orientation="Horizontal">
                             <TextBlock>Phone:</TextBlock>
                             <TextBlock Text="{Binding Path=phone}" Margin="5,0,0,0"></TextBlock>
                            </StackPanel>
                            <StackPanel Grid.Row="2" Orientation="Horizontal">
                             <TextBlock>Fax:</TextBlock>
                             <TextBlock Text="{Binding Path=faxno}" Margin="5,0,0,0"></TextBlock>
                            </StackPanel>-->
                        </Grid>
                    </Border>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

        <StackPanel VerticalAlignment="Top" Grid.Row="1" Grid.Column="1">
        <ListView x:Name="lvJobList" Height="250" VerticalAlignment="Stretch"
                  SelectedValuePath="job_num" IsSynchronizedWithCurrentItem="True"
                  SelectionChanged="GetJobItems">
            <ListView.View>
                <GridView>
                    <GridViewColumn Width="100" Header="Job No" DisplayMemberBinding="{Binding Path=job_num}"></GridViewColumn>
                    <GridViewColumn Width="100" Header="PO No" DisplayMemberBinding="{Binding Path=p_o_num}"></GridViewColumn>
                    <GridViewColumn Width="100" Header="Ship Date" DisplayMemberBinding="{Binding Path=ship_date}"></GridViewColumn>
                </GridView>
            </ListView.View>
        </ListView>
            <ContentControl x:Name="DetailView" ContentTemplate="{StaticResource DetailTemplate}" />

        <ListView x:Name="lvJobItems" Height="250" VerticalAlignment="Stretch"
                  SelectedValuePath="job_num" IsSynchronizedWithCurrentItem="True">
            <ListView.View>
                <GridView>
                    <GridViewColumn Width="100" Header="Qty" DisplayMemberBinding="{Binding Path=qty}"></GridViewColumn>
                    <GridViewColumn Width="100" Header="PartNo" DisplayMemberBinding="{Binding Path=part_no}"></GridViewColumn>
                    <GridViewColumn Width="100" Header="DwgNo" DisplayMemberBinding="{Binding Path=dwg_no}"></GridViewColumn>
                </GridView>
            </ListView.View>
        </ListView>
        </StackPanel>
    </Grid>

</Window>
Sorry, I hope I haven't bored anyone here. I'm just so excited about this I have to share it.

All comments welcomed.
Next
Reply
Map
View

Click here to load this message in the networking platform