David
David

Reputation: 413

Changing how a WPF ComboBox displays it's ItemsSource

I recently asked a question here that resulted in the following code:

<ComboBox x:Name="cmbTarget" SelectedItem="{Binding TriggerTarget}" SelectionChanged="cmbTarget_SelectionChanged">
 <ComboBox.Style>
  <Style TargetType="{x:Type ComboBox}">
   <Style.Triggers>
    <DataTrigger Binding="{Binding Text, ElementName=cmbTargetType}" Value="Material">
     <Setter Property="ItemsSource" Value="{Binding DataContext.MaterialListViewModel.MaterialViewModels.AllMaterials, RelativeSource={RelativeSource AncestorType=Window}}" />
    </DataTrigger>
    <DataTrigger Binding="{Binding Text, ElementName=cmbTargetType}" Value="ProductPart">
     <Setter Property="ItemsSource" Value="{Binding DataContext.ProductViewModel.ProductPartViewModels.AllProductParts, RelativeSource={RelativeSource AncestorType=Window}}" />
    </DataTrigger>
   </Style.Triggers>
  </Style>
 </ComboBox.Style>
</ComboBox>

Simply put, the ComboBox cmbTarget's ItemsSource is set depending on the value of ComboBox cmbTargetType. This code worked perfectly while the result of AllProductParts and AllMaterials was a collection of strings, in this case, the names of the objects in the collection. However, I realized that I don't want the result of these to be a string. I want to use the objects themselves so I can pass those to my attached ViewModel object. However, this naturally results in the ComboBox simply displaying a list of class names.

Is there way to get this ComboBox to display a ProductPart/Material's InternalName variable, while having it communicate with it's ViewModel in the form of the actual ProductPart/Material objects?

I tried using a Converter class. But it seems these are completely disconnected from the rest of the program, meaning I can't access other parts of my code to try and find the correct object belonging to an InternalName.

Upvotes: 0

Views: 1062

Answers (1)

Scott Nimrod
Scott Nimrod

Reputation: 11595

You're missing DisplayMemberPath within your combobox element. Set DisplayMemberPath to the string member of your view-model property object.

In this case, you want to set the path to "InternalName"

Upvotes: 4

Related Questions