0

I have a databound listbox which is actually displaying two columns of data. It is displayed as follows:

<UserControl.Resources>
        <DataTemplate x:Key="AlignedPairs">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="10" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>
                <TextBlock Text="{Binding Fruits}" Grid.Column="0" />
                <TextBlock Text="->" TextAlignment="Center" Grid.Column="1" />
                <TextBlock Text="{Binding Colors}" Grid.Column="3" />
            </Grid>
        </DataTemplate>
    </UserControl.Resources>

            <ListBox Name="lbStuff" Grid.ColumnSpan="2"  Grid.Row="1"
                     ItemTemplate="{StaticResource AlignedPairs}">
                <ListBox.ItemContainerStyle>
                    <Style TargetType="ListBoxItem">
                        <Setter Property="HorizontalContentAlignment" Value="Stretch" />
                    </Style>
                </ListBox.ItemContainerStyle>
            </ListBox>

Then Itemsource set in codebehind.

Based on some logic however, I would like to set either a line or a item in one of the columns, e.g. a fruit to red, or the line to bold. I have code to work out which Fruit or Color I would like to differentiate (by color/bold) in the code behind, but I can't figure out, especially given the custom listbox display, how I could go about setting a particular item to a different color/bold.

Does anyone have any ideas?

Let me know if any further code is required. Cheers.

edit: all I really want to be able to do is make the problem as easy as possible... loop through each item in the listbox and check with a string, if there is a match SOMEHOW visually distinguish this item in the listbox/listview. There is no need to make this any more complicated than it needs to be. Why you are not able to individually set an items foreground color is beyond me!

edit 2:

It looks like I almost get there with the following (but it throws exception and doesn't work)

            foreach (var li in ListView.Items)
            {

                if (someCriteria == true) 
                {
                      ((ListViewItem) li).Foreground = Brushes.Red; 
//exception throw as this is the actual object stored in this location (the Fruit object for example) not the row or listviewitem I want to change the color of so it can't be cast :(
                }
            }
2
  • How is your data modelled -- is Fruits a string, a Fruit object or a collection? What information controls the display e.g. do you have an IsPrizeWinningFruit property that you want to display as bold, or do you need to base it on the name, or what? Commented Mar 23, 2010 at 1:20
  • Fruit and Color are both objects for example, with properties Name and Code. This screen displays a relationship between the two (Fruit & Color). There are three text files fruit.txt, color.txt and relationship.txt. Application has three screens - fruit, color and relationship. Frut can add delete fruits. Color add delete colors. Relationship - can relate the two. Trying to show in this screen two things - if a fruit is in the relationship but missing from fruit.txt, and opposite, if fruit is in fruit.txt but not in relationship.txt. I already show second example with another listbox. Commented Mar 23, 2010 at 2:15

2 Answers 2

1

This is where the ViewModel design pattern really helps you out.

Presumably you have a class that exposes the properties Fruit and Color. Make a wrapper class that exposes both those properties and also exposes, say, FruitBackgroundBrush and ItemBackgroundBrush. Then you can just bind to those properties in your template, e.g.:

<DataTemplate x:Key="AlignedPairs">
    <Grid Background={Binding ItemBackgroundBrush}>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width="10" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
        <TextBlock Text="{Binding Fruits}" 
                   Background="{Binding FruitBackgroundBrush}" 
                   Grid.Column="0" />
        <TextBlock Text="->" TextAlignment="Center" Grid.Column="1" />
        <TextBlock Text="{Binding Colors}" Grid.Column="3" />
    </Grid>
</DataTemplate>
Sign up to request clarification or add additional context in comments.

1 Comment

Ah awesome! Still completely getting my head around databinding and this has made things so easy. This is the perfect solution and really helps me understand the power in databinding. Thanks very much!
0

DataTriggers will take care of this. Here's a good primer: http://en.csharp-online.net/WPF_Styles_and_Control_Templates%E2%80%94Data_Triggers

2 Comments

Will I be able to set one row, or one item in that row, color or bold property? It seems most things i'm googling say you can only set the entire listbox's font, foreground, bold properties, etc...
Also that example doesn't quite suit -> rather than an X or O, I have a potential list of string names to check against each value

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.