0

I have a ListBox in WPF Window and an image + text block as item. I am trying to set the foreground of the text block via a property (i.e. ItemForegroundColor).

ItemForegroundColor is a property in ViewModel of type SolidColorBrush, not of a property of item in AppInfoCollection.

The foreground color is not set to the intended color, rather it stays black.

<ListBox Name="lbInfoGridView" 
             ScrollViewer.VerticalScrollBarVisibility="Disabled"
             ScrollViewer.HorizontalScrollBarVisibility="Disabled"
             ScrollViewer.CanContentScroll="False" 
             Background="{Binding Path=BackgroundColor}"
             ItemsSource="{Binding AppInfoCollection}">
        <ListBox.ItemsPanel>
            <ItemsPanelTemplate>
                <UniformGrid Columns="5"
                             VerticalAlignment="Stretch"
                             HorizontalAlignment="Center"/>
            </ItemsPanelTemplate>
        </ListBox.ItemsPanel>

        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Name="spItemInfo"
                            Orientation="Horizontal"
                            HorizontalAlignment="Stretch"
                            VerticalAlignment="Stretch"
                            Margin="0 0 0 0"
                            Background="{Binding Path=BackgroundColor}">
                    <Image  Name="imgProfile" 
                            Margin="0 0 0 0"
                            Width="50" Height="100"
                            Source="{Binding ProfileImage, Converter={StaticResource binaryConverter}}"></Image>
                    <TextBlock  Name="tbName" 
                                Margin="10 0 0 0"
                                Text="{Binding Name}" 
                                Foreground="{Binding Path=ItemForegroundColor}"/>
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>

    </ListBox>

I thoughtvthe issue might be ListBox.ItemTemplate not being able to refer to ViewModel level. Hence I have tried using something like below.

Foreground="{Binding Path=MovieActorNameForegroundColor, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}} }"

Also tried ItemContainerStyle approach as mentioned in https://wildermuth.com/2007/04/17/Changing_the_Selected_Style_of_ListBox_s_in_XAML

Advice is much appreciated.

mmmrbm
  • 3
  • 2
  • `Foreground="{Binding Path=DataContext.ItemForegroundColor, RelativeSource={RelativeSource AncestorType=ListBox}}"` – Clemens Oct 10 '18 at 07:17
  • Thanks Clemens. This works. `Path=DataContext. ...` is new knowledge to me. Again much appreciated. – mmmrbm Oct 10 '18 at 07:53

1 Answers1

0

Please try to put a hard-coded color value like red or green and check if it is working. Then you will be sure that binding is not working.

Another approach:

<ListBox.Resources>
   <Style TargetType="{x:Type ListBoxItem}">
       <Setter Property="Foreground" Value="{Binding Path=MovieActorNameForegroundColor}"/>
   </Style>
</ListBox.Resources>