1

I have a ListBox that during development I had the items in the ListBox hardcoded and styled. This is how the items were styled.

<ComboBoxItem Width="Auto" Height="Auto" Content="ComboBoxItem" >
    <ComboBoxItem.Foreground>
        <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
            <GradientStop Color="#FF6F6F6F" Offset="0"/>
            <GradientStop Color="#FFD1D1D1" Offset="1"/>
        </LinearGradientBrush>
    </ComboBoxItem.Foreground>
</ComboBoxItem>

But when I set the ItemsSource property to a data object, It said my xaml was invalid. Presumably because it was adding an item through XAML.

How can I create the Style for each item, as noted in the above XAML, once you have it bound to a datasource?

Thanks.

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
John Batdorf
  • 2,502
  • 8
  • 35
  • 43
  • This should work. Something else is wrong. The exact error message would help. –  Dec 28 '08 at 01:12
  • This would not necessarily work when bound to a list of items. The XAML shown only would style the current ComboBoxItem. Each databound item would have its own ComboBoxItem, thus you would need to use styles as I describe in my answer below. – Brad Leach Dec 28 '08 at 01:19

1 Answers1

1

You can achieve this by using Styles:

<ComboBox ItemsSource="{Binding}">
  <ComboBox.Resources>
    <Style TargetType="{x:Type ComboBoxItem}">
      <Setter Property="Foreground">
        <Setter.Value>
          <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
            <GradientStop Color="#FF6F6F6F" Offset="0"/>
            <GradientStop Color="#FFD1D1D1" Offset="1"/>
          </LinearGradientBrush>
        </Setter.Value>
      </Setter>
    </Style>
  </ComboBox.Resources>
</ComboBox>

Hope this helps!

Brad Leach
  • 16,857
  • 17
  • 72
  • 88