Avatar of dma111
dma111 asked on

WPF ComboBox DataBinding

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
Microsoft Development

Avatar of undefined
Last Comment
dma111

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Gautham Janardhan

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
SOLUTION
Gautham Janardhan

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER
dma111

Thanks!  This also works but I'm not sure why:

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

The "new" instantiates an anonymous type, right -- but why doesn't just a plain SELECT work?  Is this overkill if I only want the carrier name, i.e. DisplayMemberPath="CARRIER_NA" instead of the ItemsControl.ItemTemplate?
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy