views:

629

answers:

1

I have a listbox that during development I had the items in the list box 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.

+1  A: 

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