views:

238

answers:

4

I have a combobox that I am populating with a list of parts for a Return Authorization receiving app. In our RA system, a customer can specify a return for a Kit, but only actually return part of the kit. So because of this, my combobox shows a list of parts that belong to the kit, and asks the receiver to choose which part was actually received.

I have found the defaulting the selected item in my received part list to the part specified in the return makes to lazy receivers, and incorrect part information being received. So I have left the comboxbox unselected.

What I would like to do is to highlight the specified part in the combobox, without actually selecting it. This way the actual part can be quickly found, while still requiring the user to actually choose it.

Although, this doesn't work, I think it will illustrate what I would LIKE to do.

 <ComboBox Grid.Column="1" ItemsSource="{Binding Path=Part.MasterPart.FamilyParts}" 
                      SelectedItem="{Binding Path=ReceivedPart, ValidatesOnDataErrors=True}" >
                <ComboBox.ItemContainerStyle>
                    <Style TargetType="{x:Type ComboBoxItem}">                      
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding Path=Part.MaxId}" Value="{Binding Path=Part.MaxId}" >
                                <Setter Property="Background" Value="LightSalmon" />
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </ComboBox.ItemContainerStyle>
            </ComboBox>
+1  A: 

There are ways to highlight other than setting the background color, and I'd recommend you explore them because it can be confusing to users to have different background colors for different reasons (selection versus highlighting). For example, you could put a little star next to relevant items or make them bold.

That said, You can just do this to set the background color of the ComboBoxItem:

<ComboBox.ItemContainerStyle>
    <Style TargetType="ComboBoxItem">
        <Setter Property="Background" Value="{Binding Part.MaxId, Converter={StaticResource BackgroundConverter}}"/>
    </Style>
</ComboBox.ItemContainerStyle>

Even better, use a view model and just bind directly to a BackgroundColor property:

<ComboBox.ItemContainerStyle>
    <Style TargetType="ComboBoxItem">
        <Setter Property="Background" Value="{Binding BackgroundColor}"/>
    </Style>
</ComboBox.ItemContainerStyle>

HTH, Kent

Kent Boogaart
Kent, I like your suggestion about some other method of highlighting. I believe your right about not having diffent highlighted colors in a comboxbox because it may confuse the issue.
Russ
A: 

You're on the right track, but what I would do is put a bool read-only property in your Part class that told the combobox whether it should be highlighted in this instance. You could try something like this:

<ComboBox Grid.Column="1" ItemsSource="{Binding Path=Part.MasterPart.FamilyParts}" 
          SelectedItem="{Binding Path=ReceivedPart, ValidatesOnDataErrors=True}" >
  <ComboBox.ItemTemplate>
    <DataTemplate>
      <Grid>
        <Border Background="LightSalmon" Visibility="{Binding Part.Highlighted, Converter={StaticResource BoolToVizConverter}}"/>
        <TextBlock Text="{Binding Part.Name}"/>
      </Grid>
    </DataTemplate>
  </ComboBox.ItemTemplate>
</ComboBox>

This way, the background of the border wouldn't display at all if Highlighted were false.

opedog
+2  A: 

You've got the right idea. The only thing I see wrong with your code is the DataTrigger attributes.

If Value was, well, just a value, it would work:

<DataTrigger Binding="{Binding Path=Part.MaxId}" Value="999" >

I would wrap this logic up into a new property on the viewmodel for simplicity:

<DataTrigger Binding="{Binding Path=Part.ShouldHighlight}" Value="true">
Rob Fonseca-Ensor
A: 

Instead of adding a property to the view model, you could use a Style Selector (http://msdn.microsoft.com/en-us/library/system.windows.controls.styleselector.aspx) to determine which style to use for an item.

Marc