Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3981670
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T05:26:16+00:00 2026-05-20T05:26:16+00:00

I have a ListBox in a WP7 app page which I bind to a

  • 0

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();
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T05:26:17+00:00Added an answer on May 20, 2026 at 5:26 am

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a ListBox that uses a WrapPanel for its ItemsPanel , a custom
I have a listbox that in which every item is represented using a textbox.
I have a listbox that is populated with a list of objects that has
I have a ListBox bound to a collection of custom objects. I have an
I have a ListBox which binds to a child collection on a ViewModel. The
I have a listbox bound to a collection. I would like the ListBox to
I have a listbox with a datatemplate for the items. The problem is that
I have a ListBox that uses DataTemplateSelector to dynamically decide what template to use
I have a Listbox that I binds to a resource (sort) CollectionViewSource in my
I have a Listbox with an itemssource set to an ObservableCollection of DataRow. Let's

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.