I've been banging my head on this WPF issue for a couple days. Everything looks fine to me, so I don't understand what I'm doing wrong. I had to change the code, which had been working fine, to satisfy a business requirement. When adding a new record to the table the WPF ComboBox has to show the text "Select...". I've asked around for guidance as to how to do that. The best solution I found, on Stack Overflow, was to use a ContentControl. This is what I have now:
<ContentControl DataContext="{Binding}">
<ContentControl.ContentTemplate>
<DataTemplate>
<Grid>
<ComboBox Width="200"
x:Name="InstrumentModelComboBox"
IsSynchronizedWithCurrentItem="True"
ItemsSource="{Binding InstrumentModelList}"
DisplayMemberPath="Model"
SelectedValuePath="ID"
SelectedValue="{Binding InstrumentModelID, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
</ComboBox>
<TextBlock x:Name="InstrumentModelTextBlock"
Text="Select..."
IsHitTestVisible="False"
Visibility="Collapsed" />
</Grid>
<DataTemplate.Triggers>
<Trigger SourceName="InstrumentModelComboBox"
Property="SelectedItem"
Value="{x:Null}">
<Setter TargetName="InstrumentModelTextBlock"
Property="Visibility"
Value="Visible" />
</Trigger>
</DataTemplate.Triggers>
</DataTemplate>
</ContentControl.ContentTemplate>
</ContentControl>
This works fine when the user wishes to create a new record. Exactly what it was supposed to do. But it fails to show an existing record when the user wishes to edit the record. Instead, it still shows the TextBlock with "Select..." in it.
My guess is I've done something wrong but am not sure where. Here's the InstrumentModelID property from the viewmodel:
public long? InstrumentModelID
{
get
{
if (Course != null)
{
return Course.InstrumentModelID;
}
return 0;
}
set
{
Course.InstrumentModelID = value;
RaisePropertyChanged();
}
}
I've set a breakpoint both in the first line of the setter and the getter. The setter always gets called. But the getter never gets called, as I would expect it to be because of my referencing it in the XAML.
I'm working with VS 2019. We're using .NET Framework 4.5.2.
So, what's the mistake that I'm making, please?
