1

I have a ListBox that correctly populates with an array of an object. This is using a DataTemplate with the DataType set to my object and a TextBlock with `Text="{Binding Name}". This works correctly and I'm not having issues with it populating.

My problem is with a ComboBox that I'm seemingly populating the same way. I set the DataType and {Binding Name} (to a different object type than the ListBox. The ViewModel structure and code-behind binding are extremely similar for each one. I'm having a hell of a time trying to figure out how to get it to populate.

I'm also using MaterialDesignInXAML for the ComboBox with Style="{StaticResource MaterialDesignFloatingHintComboBox}". I don't think that should matter, just adding it here in case it does.

I've looked into some questions with answers that should cover this, but I can't seem to get it to work.

https://stackoverflow.com/a/56782702/9452665

https://stackoverflow.com/a/45133895/9452665

Here's the relevant ListBox code:

<ListBox x:Name="NavigationItemsListBox" Margin="0 16 0 16" SelectedIndex="0">
    <ListBox.Resources>
        <Style TargetType="ScrollBar" BasedOn="{StaticResource MaterialDesignScrollBarMinimal}" />
    </ListBox.Resources>
    <ListBox.ItemTemplate>
        <DataTemplate DataType="model:NavigationItem">
            <TextBlock Text="{Binding Name}" Margin="32 0 32 0" />
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>
this.OneWayBind(ViewModel,
    viewModel => viewModel.NavigationItems,
    view => view.NavigationItemsListBox.ItemsSource)
    .DisposeWith(disposableRegistration);

MainWindowViewModel has an array of NavigationItems.

Here's the relevant ComboBox code:

<ComboBox Name="DataTypeComboBox" Style="{StaticResource MaterialDesignFloatingHintComboBox}" materialDesign:HintAssist.Hint="Data Type" Width="300" HorizontalAlignment="Left" Margin="8">
    <ComboBox.ItemTemplate>
        <DataTemplate DataType="models:DatasheetType">
            <TextBlock Text="{Binding Name}"/>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>
this.OneWayBind(ViewModel,
    viewModel => viewModel.DatasheetTypeItems,
    view => view.DataTypeComboBox.ItemsSource)
    .DisposeWith(disposableRegistration);

DataImportViewModel has an array of DatasheetTypes.

Tried a bunch of things but can't seem to get the ComboBox to actually show any items in it.

Taryn
  • 206
  • 1
  • 6
  • The problem was I was not assigning the view model in the code behind for the usercontrol hosting the ComboBox. – Taryn Aug 10 '19 at 18:07

1 Answers1

1

The problem was that there was no assignment of ViewModel to the view model in the UserControl code-behind hosting the ComboBox. After adding the assignment it works.

ViewModel = new DataImportViewModel();
Taryn
  • 206
  • 1
  • 6