0

I've got a ComboBox in a WPF app that I populate from a lookup table. The requirements say that if the user has chosen to create a new record into a data type, that the ComboBox is to display the text "Select...". That text isn't in the lookup table, so I create a dummy record and insert it into the ObservableCollection used to populate the ComboBox like so:

var newTmpCert = new CertificationType()
{
    ID = 0,
    CertType = SELECT_DROPDOWN_STRING, //SELECT_DROPDOWN_STRING == "Select..."
    Inactive = false
};
CertTypeList.Insert(0, newTmpCert);

CertTypeList is defined like this:

private ObservableCollection<CertificationType> certTypeList;
public ObservableCollection<CertificationType> CertTypeList 
{ 
    get { return certTypeList; }
    set
    {
        certTypeList = value;
        RaisePropertyChanged();
    }
}

The assignment that I've been struggling with to make the ComboBox show "Select..." is this:

Course new_record = new Course();
new_record.CertificationLevel = "Re-certification";
Course = new_record;
CertificationTypeID = 0;

And the XAML for the ComboBox is this:

<ComboBox  Width="200"
           IsSynchronizedWithCurrentItem="True"
           ItemsSource="{Binding CertTypeList}"
           DisplayMemberPath="CertType"
           SelectedValuePath="ID"
           SelectedValue="{Binding CertificationTypeID, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">

Every time I test the app and select to create a new Certification record, the ComboBox is always blank. So, I thought I'd try something different, and assign CertificationTypeID to 2 (there's a third record in that lookup table), that works perfectly.

Why does it work? The ObservableCollection should raise a propertychanged event. The ComboBox is not bound to the lookup table in the database, it is bound to the ObservableCollection. Why does it work, if I specify an existing record in the lookup table, but doesn't work if I assign 0 to CertificationTypeID in the viewmodel?

Rod
  • 4,107
  • 12
  • 57
  • 81
  • what is the initial value of CertificationTypeID? is it 0? maybe try `var newTmpCert = new CertificationType() { ID = -1, ...`, `CertificationTypeID = -1;` to ensure there is a change – ASh Dec 04 '20 at 19:32
  • also: https://stackoverflow.com/questions/1426050/how-to-display-default-text-select-team-in-combo-box-on-pageload-in-wpf – ASh Dec 04 '20 at 19:32
  • @ASh the initial value is -1, before I add the "Select..." text. Also, I have a need to dynamically add and remove "Select..." depending upon whether the user wants to create a new course or edit an existing course. Our practice is to have a user control with a tab control on it. The first tab is a listing of all courses. Clicking the Add button takes the user to tab 2, where they edit that detail. Click the Add button takes the user to tab 2 where they are to start adding a new course. – Rod Dec 04 '20 at 20:17
  • @ASh, I've tried implementing one of the suggestions in the SO post you linked to, the one given by HappyNomad. That means, I believe, that I've got to not use the logic I've written to add and remove the "Select..." string to the ObservableCollection. I've done that, but now when I go to add or edit a course, the combobox doesn't display anything when I open it. :( – Rod Dec 04 '20 at 21:17

1 Answers1

1

If you are happy with the HappyNomad solution, I think it is just a binding problem. Have you checked the Output-window for any error message from XAML (when running the program). I made an example with that solution and the ContentControl XAML looks like this:

<ContentControl Content="{Binding}">
    <ContentControl.ContentTemplate>
        <DataTemplate>
            <Grid>
                <ComboBox x:Name="cb" Width="200" HorizontalAlignment="Left"
                   IsSynchronizedWithCurrentItem="True"
                   ItemsSource="{Binding CertTypeList}"
                   DisplayMemberPath="CertType"
                   SelectedValuePath="ID"
                   SelectedValue="{Binding CertificationTypeID, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
                <TextBlock x:Name="tb" Text="Select..." IsHitTestVisible="False" Visibility="Collapsed"/>
            </Grid>
            <DataTemplate.Triggers>
                <Trigger SourceName="cb" Property="SelectedItem" Value="{x:Null}">
                    <Setter TargetName="tb" Property="Visibility" Value="Visible"/>
                </Trigger>
            </DataTemplate.Triggers>
        </DataTemplate>
    </ContentControl.ContentTemplate>
</ContentControl>
RolandJS
  • 717
  • 5
  • 5
  • Thank you, @RolandJS. That took care of it. Now I'm encountering a different error, but this issue is resolved. Thank you again. :) – Rod Dec 07 '20 at 16:55