Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

Troubleshooting
Research
Professional Opinions
Ask a Question
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

troubleshooting Question

WPF ComboBox DataBinding

Avatar of dma111
dma111 asked on
Microsoft Development
3 Comments1 Solution16055 ViewsLast Modified:
I would appreciate any help with databinding a ComboBox in WPF  and with any other suggestions you might have about how Ive gone about this app.  (Thanks in advance!)

1.      A master-detail WPF application uses four tables: EMPLOYEE (the Master), DEPEND (Dependent, the Detail), CARRIER  and GROUP (lookups).  A ComboBox is bound in XAML to CARRIER (two fields: CARRCODE and CARRIER_NA (Name) via ItemsSource but fails to databind (see screenshot in the attached doc):

<GridViewColumn Header="Carrier" Width="100">
    <GridViewColumn.CellTemplate>
        <DataTemplate>
            <ComboBox
                  SelectedValuePath="CARRCODE" IsEditable="False"
                  SelectedValue="{Binding Path=CARRCODE}"  
                  Margin="-6,0,-6,0" Width="Auto"
                  x:Name="cboCarrier"
                  IsSynchronizedWithCurrentItem="False"      
                  ItemsSource="{Binding Source={StaticResource CarrierView}}">
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal" >
                            <TextBlock Name="Carrier"
                               Text="{Binding Path=CARRIER_NA}" />
                            <TextBlock Width="5"> ( </TextBlock>
                            <TextBlock Name="Code"
                               Text="{Binding Path=CARRCODE}" />
                            <TextBlock Width="5">)</TextBlock>
                        </StackPanel>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ComboBox>
        </DataTemplate>
    </GridViewColumn.CellTemplate>
</GridViewColumn>

2.      StaticResource CarrierView is set in App.xaml with other CollectionViewSources as follows:

    <Application.Resources>
        <CollectionViewSource x:Key="MasterView" />
        <CollectionViewSource Source="{Binding Source={StaticResource MasterView}, Path='DEPENDs'}" x:Key="DetailView" />
        <CollectionViewSource x:Key="GroupView" />
        <CollectionViewSource x:Key="CarrierView" />
    </Application.Resources>

3.      Here is the code-behind:

public partial class Window1 : Window
{
    private Billing03DataContext db = new Billing03DataContext();
    private IEnumerable<EMPLOYEE> EEData;

    private CollectionViewSource MasterViewSource;
    private CollectionViewSource DetailViewSource;
    private CollectionViewSource CarrierViewSource;
    private CollectionViewSource GroupViewSource;

    private BindingListCollectionView MasterView;
    private BindingListCollectionView DetailView;
    private BindingListCollectionView CarrierView;
    private BindingListCollectionView GroupView;

    public Window1()
    {
        InitializeComponent();
    }

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        this.EEData = db.EMPLOYEEs;

        var carrierList = from c in db.CARRIERs
                          select c.CARRIER_NA;  

        var groupList = from g in db.GROUPs
                        select g.GROUPNAME;
       
        this.MasterViewSource = (CollectionViewSource)this.FindResource("MasterView");
        this.DetailViewSource = (CollectionViewSource)this.FindResource("DetailView");
        this.CarrierViewSource = (CollectionViewSource)this.FindResource("CarrierView");
        this.GroupViewSource = (CollectionViewSource)this.FindResource("GroupView");
       
        MasterViewSource.Source = this.EEData;
        CarrierViewSource.Source = carrierList;
        GroupViewSource.Source = groupList;

        this.MasterView = (BindingListCollectionView)this.MasterViewSource.View;
        this.MasterView.CurrentChanged += new EventHandler(MasterView_CurrentChanged);
        this.DetailView = (BindingListCollectionView)this.DetailViewSource.View;
        this.CarrierView = (BindingListCollectionView)this.CarrierViewSource.View;
        this.GroupView = (BindingListCollectionView)this.GroupViewSource.View;
    }

    void MasterView_CurrentChanged(object sender, EventArgs e)
    {
        this.DetailView = (BindingListCollectionView)this.DetailViewSource.View;
    }

 



ComboBox-Problem.doc
ASKER CERTIFIED SOLUTION
Avatar of Gautham Janardhan
Gautham Janardhan

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Commented:
This problem has been solved!
Unlock 1 Answer and 3 Comments.
See Answers