Combo box selected item not updating

MattNorman

Well-known member
Joined
May 22, 2021
Messages
98
Programming Experience
1-3
I am having some issues with getting my combo boxes selected item to display correctly.

I have a view model called 'ReportSkillCallProfileViewModel '. This view model has a property of type 'ReportSkillCallProfileFilterModel '. The 'ReportSkillCallProfileFilterModel ' the has a property called 'ReportInterval' which is an Enum of type 'ReportIntervals':

Main report view model:
public class ReportSkillCallProfileViewModel : BaseViewModel
{
    public ReportSkillCallProfileFilterModel FilterData { get; set; } = new ReportSkillCallProfileFilterModel();

    private void ShowFilterDialog(object param)
    {
        DialogContent = new ReportSkillCallProfileFilterView(FilterData, this);
        DialogOpen = true;
    }
}

Report filter model:
public class ReportSkillCallProfileFilterModel : BaseViewModel
{
    public ReportIntervals ReportInterval { get; set; }
}

The constructor of the report filter view model takes a parameter of type ReportSkillCallProfileFilterModel which is passed through from the main report view model and then bound to a property within the report filter view model.

It also has a list of strings that is used as the ItemSource for the combo box on the filter view.

Report filter view model:
public class ReportSkillCallProfileFilterViewModel : BaseViewModel
{
    public ReportSkillCallProfileFilterModel FilterData { get; set; } = new ReportSkillCallProfileFilterModel();
   
    public ObservableCollection<string> IntervalTypes { get; set; } = new ObservableCollection<string>(new List<string> { "Intraday", "Daily", "Weekly", "Monthly" });
}

Here is the XAML for the combo box in the filter view:

Filter view:
<ComboBox
    Grid.Column="3"
    Grid.Row="3"
    Width="250"
    HorizontalAlignment="Left"
    Height="30"
    VerticalContentAlignment="Bottom"
    ItemsSource="{Binding IntervalTypes}"
    SelectedItem="{Binding FilterData.ReportInterval}"
    IsSynchronizedWithCurrentItem="True">
</ComboBox>

If I change the selected combo box item this updates the source as far back as the ReportSkillCallProfileViewModel and running the report uses the correct selection.

The issue I have is when I re-open the filter view, the selected item defaults back to the first item in the list and not the one that the bound property is set to.

If I leave it as the default and run the report, I can see that it is not using the default value, it is using the value that was previously selected.

I am guessing this may be something to do with the fact that the selected item is an Enum type and the bound list is a list of strings.

Can anyone offer any advice on this?

Regards
Matt
 
I have managed to resolve this by using the following as the ItemSource instead of the string collection:

C#:
public List<ReportIntervals> ReportIntervalList { get; set; } = Enum.GetValues(typeof(ReportIntervals)).Cast<ReportIntervals>().ToList();
 
Back
Top Bottom