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.CellTempla
te>
<DataTemplate>
<ComboBox
SelectedValuePath="CARRCOD
E" IsEditable="False"
SelectedValue="{Binding Path=CARRCODE}"
Margin="-6,0,-6,0" Width="Auto"
x:Name="cboCarrier"
IsSynchronizedWithCurrentI
tem="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.ItemTemplat
e>
</ComboBox>
</DataTemplate>
</GridViewColumn.CellTempl
ate>
</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
.FindResou
rce("Maste
rView");
this.DetailViewSource = (CollectionViewSource)this
.FindResou
rce("Detai
lView");
this.CarrierViewSource = (CollectionViewSource)this
.FindResou
rce("Carri
erView");
this.GroupViewSource = (CollectionViewSource)this
.FindResou
rce("Group
View");
MasterViewSource.Source = this.EEData;
CarrierViewSource.Source = carrierList;
GroupViewSource.Source = groupList;
this.MasterView = (BindingListCollectionView
)this.Mast
erViewSour
ce.View;
this.MasterView.CurrentCha
nged += new EventHandler(MasterView_Cu
rrentChang
ed);
this.DetailView = (BindingListCollectionView
)this.Deta
ilViewSour
ce.View;
this.CarrierView = (BindingListCollectionView
)this.Carr
ierViewSou
rce.View;
this.GroupView = (BindingListCollectionView
)this.Grou
pViewSourc
e.View;
}
void MasterView_CurrentChanged(
object sender, EventArgs e)
{
this.DetailView = (BindingListCollectionView
)this.Deta
ilViewSour
ce.View;
}
ComboBox-Problem.doc