DataBound WPF ListBox Styling on ListBoxItems
I have ListBox
that during development I had elements in ListBox
hardcoded and styled. This is how the elements 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 property ItemsSource
to the data object, it said my xaml is not valid. Presumably because he was adding the element via XAML.
How can I create Style
for each element as stated in the above XAML once you have bound to a data source?
Thank.
+1
source to share
1 answer
You can achieve this 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!
+1
source to share