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 (Location 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();
}
Here is a way….i think it works
The property you want to bind is
Foreground, soIn the program put up the apt condition where you populate your listbox to choose the right Forground using
foregroundColorPopulate the list box using a class containin
create a
List<listboxItem> items=new List<listboxItem>();and fill the list
itemsin a loop where u give the condition you want.after that
call
lbxSavedLocs.ItemSource=items