Reputation: 3
i'm facing a kindof stupid problem: i'm not able to change the Foreground color of a ListBoxItem with a DataTemplate. I'm sure theres a really simple solution for this but i just don't see it!
Here is my code:
<ListBox Height="181" HorizontalAlignment="Left" Margin="12,53,0,0"
Name="lstKeys" VerticalAlignment="Top" Width="491">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Margin="3">
<DockPanel>
<Image Width="32" Height="32" Stretch="Fill" Margin="3"
HorizontalAlignment="Left" Name="image3" VerticalAlignment="Top"
Source="{Binding Icon}" />
<DockPanel Margin="3">
<TextBlock Text="{Binding Product}" Foreground="Green"
DockPanel.Dock="Top" />
<TextBlock Text="{Binding Key}" Foreground="Black"
FontWeight="Bold" DockPanel.Dock="Bottom" />
</DockPanel>
</DockPanel>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
At runtime the color ist the default System Color. Setting the Foreground Color directly by ListBox-Attribute doesn't work, i hope somebody can help.
Thanks in advance!
Upvotes: 0
Views: 1600
Reputation: 22435
hi if i use your listbox its work like you want :) To know that the forecolor is working i changed the datatemplate to a fixed string in the textblock.
does your "green" Textblock show any text?
<ListBox ItemsSource="{Binding Items}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Margin="3">
<DockPanel>
<Image Width="32" Height="32" Stretch="Fill" Margin="3"
HorizontalAlignment="Left" Name="image3" VerticalAlignment="Top"
Source="{Binding Icon}" />
<DockPanel Margin="3">
<TextBlock Text="the binding seems to be wrong" Foreground="Green"
DockPanel.Dock="Top" />
<TextBlock Text="{Binding Key}" Foreground="Black"
FontWeight="Bold" DockPanel.Dock="Bottom" />
</DockPanel>
</DockPanel>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
EDIT: i know that problem :)
i override the SystemColors to get what i want.
<ListBox>
<ListBox.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="LightBlue"/>
</ListBox.Resources>
</ListBox>
Upvotes: 1