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