How to set foreground color of ListBox item
I have a ListBox in a WP7 app page which I bind to a collection (List) of a custom object called Location. In that object is a field called WMO and what I want to do when the ListBox loads is set the foregound color of whatever bound listbox item has that same value as my default value... but I just can't seem to get this working and nothing I've read or googled has helped.
I know the items in the list are bound to the datasource but I want to get to the physical representation of that item and change the foreground colour.... just can't work out how I do that so if anyone can help I'd appreciate it.
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="0,0,0,0" >
<ScrollViewer Height="615" HorizontalAlignment="Left" Margin="5,5,5,5" Name="scrollViewer1" VerticalAlignment="Top">
<ListBox Name="lbxSavedLocs" Height="615" FontSize="22" HorizontalAlignment="Left" VerticalAlignment="Top" Width="470" SelectionChanged="lbxSavedLocs_SelectionChanged" Loaded="lbxSavedLocs_Loaded">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Width="380" Text="{Binding SiteName}" HorizontalAlignment="Left" />
<TextBlock Width="90" Text="{Binding WMO}" HorizontalAlignment="Center" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</ScrollViewer>
</Grid>
private void lbxSavedLocs_Loaded(object sender, RoutedEventArgs e)
{
//Populate the listbox from our saved locations.
lbxSavedLocs.ItemsSource = gl.savedLocs.OrderBy(x => x.SiteName);
foreach (Locatio开发者_JS百科n itm in lbxSavedLocs.Items)
{
if (loc.WMO == gl.defaultWMO)
{
//GET AN "INVALID CAST" EXCEPTION HERE:
((ListBoxItem)itm).Foreground = new SolidColorBrush(Color.FromArgb(255, 255, 255, 255));
}
}
//Hopefully this produces a redraw of the ListBox.
lbxSavedLocs.InvalidateArrange();
}
Try this:
Option1:
ListBoxItem lbi1 = (ListBoxItem)(listBox.ItemContainerGenerator.ContainerFromIndex(0));
lbi1.Foreground= new SolidColorBrush(Color.FromArgb(100, 45, 23, 45));
Option2:
ListBoxItem lbi2 = (ListBoxItem)(listBox.ItemContainerGenerator.ContainerFromItem(listBox.Items.SelectedItem));
lbi2.Foreground= new SolidColorBrush(Colors.Red);
If you need to apply the same foreground colour to all items in the ListBox
or to bind the foreground colour to a value in the data item, then the best approach is to modify the ItemContainerStyle
. The ItemContainerStyle
defines the visual wrapper around the contents of the ItemTemplate
and by default uses a ContentControl
that you can set or bind the Foreground
property:
<Style x:Key="ListBoxItemStyle1" TargetType="ListBoxItem">
<Setter Property="Background" Value="Transparent"/>
<Setter Property="BorderThickness" Value="0"/>
<Setter Property="BorderBrush" Value="Transparent"/>
<Setter Property="Padding" Value="0"/>
<Setter Property="HorizontalContentAlignment" Value="Left"/>
<Setter Property="VerticalContentAlignment" Value="Top"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<Border x:Name="LayoutRoot"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
Background="{TemplateBinding Background}"
HorizontalAlignment="{TemplateBinding HorizontalAlignment}"
VerticalAlignment="{TemplateBinding VerticalAlignment}">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal"/>
<VisualState x:Name="MouseOver"/>
<VisualState x:Name="Disabled">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background"
Storyboard.TargetName="LayoutRoot">
<DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource TransparentBrush}"/>
</ObjectAnimationUsingKeyFrames>
<DoubleAnimation Duration="0"
To=".5"
Storyboard.TargetProperty="Opacity"
Storyboard.TargetName="ContentContainer"/>
</Storyboard>
</VisualState>
</VisualStateGroup>
<VisualStateGroup x:Name="SelectionStates">
<VisualState x:Name="Unselected"/>
<VisualState x:Name="Selected"/>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<ContentControl x:Name="ContentContainer"
ContentTemplate="{TemplateBinding ContentTemplate}"
Content="{TemplateBinding Content}"
Foreground="#FFFF0000"
HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
Margin="{TemplateBinding Padding}"
VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}" Background="Black"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Here is a way....i think it works
The property you want to bind is Foreground
, so
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="0,0,0,0" >
<ScrollViewer Height="615" HorizontalAlignment="Left" Margin="5,5,5,5" Name="scrollViewer1" VerticalAlignment="Top">
<ListBox Name="lbxSavedLocs" Height="615" FontSize="22" HorizontalAlignment="Left" VerticalAlignment="Top" Width="470" SelectionChanged="lbxSavedLocs_SelectionChanged" Loaded="lbxSavedLocs_Loaded">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Width="380" Text="{Binding SiteName}" Foreground="{binding forgroundColor}" HorizontalAlignment="Left" />
<TextBlock Width="90" Text="{Binding WMO}" Foreground="{binding forgroundColor}" HorizontalAlignment="Center" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</ScrollViewer>
</Grid>
In the program put up the apt condition where you populate your listbox to choose the right Forground using foregroundColor
Populate the list box using a class containin
class lisboxItem
{
public string SiteName{get;set;}
public string forgroundColor{get;set;}
public string WMO{get;set;}
}
create a List<listboxItem> items=new List<listboxItem>();
and fill the list items
in a loop where u give the condition you want.
after that
call
lbxSavedLocs.ItemSource=items
精彩评论