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 7038717
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:42:09+00:00 2026-05-28T01:42:09+00:00

I have the following xaml code: <ListBox Foreground={Binding MyColor, Converter={local:ColorConverter}} ItemsSource={Binding LogCollection, Mode=TwoWay} Grid.Row=1>

  • 0

I have the following xaml code:

<ListBox Foreground="{Binding MyColor, Converter={local:ColorConverter}}" ItemsSource="{Binding LogCollection, Mode=TwoWay}" Grid.Row="1">
</ListBox>

This changes the foreground color for the entire listbox, so I modified the code in this way:

<ListBox ItemsSource="{Binding LogCollection, Mode=TwoWay}" Grid.Row="1">
   <ListBox.ItemTemplate>
      <DataTemplate>
      <TextBlock Foreground="{Binding MyColor, Converter={local:ColorConverter}}"  Text="{Binding}"/>
      </DataTemplate>
   </ListBox.ItemTemplate>
</ListBox>

In this way I wanted to set the foreground for an item instead for the entire listbox, but it is not working. How do I find the right datacontext ? MyColor is a property on my MainViewModel.

LATER EDIT WITH THE SOLUTION

Jens’s answer was the one that showed me where I was wrong. Instead of storing simple message log strings in the ObservableCollection, I created a new class (LogItems) which contains a Message and a Color members. Now the LogCollection is typeof LogItems instead of strings.

I populate the listbox with the following code in my viewmodel:

LogItems logitem = new LogItems(myMessage, myColor);
LogCollection.Insert(0, logitem);

And the view has the following form. Also it doesn’t require anymore to use RelativeSource, because the datacontext is the same.

<ListBox ItemsSource="{Binding LogCollection, Mode=TwoWay}" Grid.Row="1">                        
 <ListBox.ItemTemplate>                             
  <DataTemplate>
   <TextBlock Foreground="{Binding Path=Color, Converter={local:ColorConverter}}" Text="{Binding Path=Message}"/>
  </DataTemplate>
 </ListBox.ItemTemplate>
</ListBox>              

Thank you all for your answers which lead me to this solution.

  • 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-28T01:42:10+00:00Added an answer on May 28, 2026 at 1:42 am

    The DataContext of generated container in a listbox is automatically set to the corresponding item, therefore your Binding does not find the Property MyColor. You need to use a RelativeSource binding to bind to the DataContext of the containing list:

    <TextBlock Foreground="{Binding DataContext.MyColor, 
                                    RelativeSource={RelativeSource 
                                            Mode=FindAncestor, 
                                            AncestorType={x:Type ListBox}}, 
                                    Converter={local:ColorConverter}}"  
               Text="{Binding}"/>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following XAML code: <ListBox ItemsSource={Binding Languages}> <ListBox.ItemTemplate> <DataTemplate> <StackPanel> <Image Source={Binding
I have a user control with the following XAML: <ScrollViewer> <ListBox ItemsSource={Binding Items}> <ListBox.ItemTemplate>
I have the following XAML: <ListBox HorizontalAlignment=Left Margin=0,6,0,10 Name=listBox1 Width=468 ItemsSource={Binding} Grid.ColumnSpan=1> <ItemsControl.ItemTemplate> <DataTemplate>
I have a following XAML: <ComboBox Name=groupComboBox ItemsSource={Binding Path=MyServiceMap.Groups} DisplayMemberPath={Binding Name}/> In the code
I have following XAML: <TextBlock HorizontalAlignment=Center VerticalAlignment=Center FontSize=10 FontFamily=Arial Foreground=#414141> <Run Text={Binding LoadsCount} />
I have a following XAML: <DataGrid Name=nodeDataGrid ItemsSource={Binding NodeList} AutoGenerateColumns=False RowBackground=White AlternatingRowBackground=AliceBlue Grid.Column=1 HorizontalAlignment=Stretch
I have the following xaml-Code: <Border x:Name=border> <Border.Style> <Style TargetType={x:Type Border}> <Style.Triggers> <DataTrigger Binding={Binding
I have the following XAML code, having removed the styling and formatting tags: <ListBox
I have the following column defined in my xaml code <telerik:GridViewMaskedTextBoxColumn Mask=P1 MaskType=Numeric DataMemberBinding={Binding
I have the following XAML code: <phone:PhoneApplicationPage.ApplicationBar> <shell:ApplicationBar IsVisible=True IsMenuEnabled=False x:Name=PageBar> <shell:ApplicationBarIconButton IconUri=/Assets/Icons/appbar.questionmark.rest.png Text=Help

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.