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

  • Home
  • SEARCH
  • 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 5964337
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T19:24:49+00:00 2026-05-22T19:24:49+00:00

Here’s what i’ve tried so far to achieve gradient background in a listbox item

  • 0

Here’s what i’ve tried so far to achieve gradient background in a listbox item (List) depending on a int value of a databinded object

My object in its simplified form:

public class Item {
 public string name { get; set; }
 public string address { get; set; }
 public int highlight { get; set; }
}

Converter Attempt:

Using this Converter:

public class BusinessTypeToBackgroundConverter : IValueConverter
{
    private static readonly LinearGradientBrush NormalBkg = new LinearGradientBrush
    {
        StartPoint = new Point(0, 0),
        EndPoint = new Point(0, 1),
        GradientStops = new GradientStopCollection
                            {
                                new GradientStop {Color = Util.GetColorFromHex("#4ce6e6e6")},
                                new GradientStop {Color = Util.GetColorFromHex("#ffe6e6e6")}
                            }
    };

    private static readonly LinearGradientBrush HighlightedBkg = new LinearGradientBrush
    {
        StartPoint = new Point(0, 0),
        EndPoint = new Point(0, 1),
        GradientStops = new GradientStopCollection
                                                {
                                                    new GradientStop {Color = Util.GetColorFromHex("#4cffffcc")},
                                                    new GradientStop {Color = Util.GetColorFromHex("#ffffffcc")}
                                                }
    };

    public object Convert(object value, Type targetType,
                    object parameter, CultureInfo culture)
    {
        switch ((int)value)
        {
            case 1:
                return HighlightedBkg;
            case 2:
                return NormalBkg;
            default:
                return NormalBkg;
        }
    }

    public object ConvertBack(object value, Type targetType,
                              object parameter, CultureInfo culture)
    {
        throw new NotImplementedException("BusinessTypeToBackgroundConverter ConvertBack Method Not Implemented");
    }
}

And this Item template

<ListBox                     
Name="lstResults" 
ItemContainerStyle="{StaticResource ListBoxItemStyle1}">
<ListBox.ItemTemplate>
    <DataTemplate>                           
        <Grid Background="{Binding highlight, Converter={StaticResource myConverter}}">
            <StackPanel>
                <TextBlock Text="{Binding name}" TextWrapping="Wrap" FontSize="24" FontWeight="Bold" Foreground="Black"/>
                <TextBlock Text="{Binding address}" TextWrapping="Wrap" FontSize="24" Foreground="Black" />
            </StackPanel>
        </Grid>
    </DataTemplate>
</ListBox.ItemTemplate>
</ListBox>

Code Behind Attempt

Added a “LinearGradientBrush background” property to my Item object

public LinearGradientBrush background
{
    get
    {
        if (highlight == 1) return HighlightedBkg;
        else return NormalBkg;
    }
}

In both cases only the Start color of the Gradient is applied to the listItem (Grid Background). So i end up with a solid color 🙂

Is there anyway to set the background ti a gradient from code and not using the XAML notation:

<LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
    <GradientStopCollection>
        <GradientStop Color="#ff444444" Offset="0" />
        <GradientStop Color="#ff000000" Offset="1" />
    </GradientStopCollection>
</LinearGradientBrush>
  • 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-22T19:24:50+00:00Added an answer on May 22, 2026 at 7:24 pm

    The problem is that when you specify your Gradient stops in code you are not specifying the offset.

    However I would suggest the you not avoid Xaml for a solution. First read this blog: A Generic Boolean Value Converter. I would also suggest that your Hightlight property ought to be a bool type not an int.

    By including the converter code from the blog in your project you should be in a position to do something like this:-

    <Grid x:Name="LayoutRoot">
       <Grid.Resources>
           <local:BoolToBrushConverter x:Key="Highlighter">
                <local:BoolToBrushConverter.TrueValue>
                    <LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
                      <GradientStopCollection>
                    <GradientStop Color="#4cffffcc" Offset="0" />
                    <GradientStop Color="#ffffffcc" Offset="1" />
                      </GradientStopCollection>
                     </LinearGradientBrush>
                </local:BoolToBrushConverter.TrueValue>
                <local:BoolToBrushConverter.FalseValue>
                    <LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
                      <GradientStopCollection>
                    <GradientStop Color="#4ce6e6e6" Offset="0" />
                    <GradientStop Color="#ffe6e6e6" Offset="1" />
                      </GradientStopCollection>
                     </LinearGradientBrush>
                </local:BoolToBrushConverter.FalseValue>
           </local:BoolToBrushConverter>
       </Grid.Resources>
    
    <ListBox                     
    Name="lstResults" 
    ItemContainerStyle="{StaticResource ListBoxItemStyle1}">
    <ListBox.ItemTemplate>
        <DataTemplate>                           
            <Grid Background="{Binding highlight, Converter={StaticResource Highlighter}}">
                <StackPanel>
                    <TextBlock Text="{Binding name}" TextWrapping="Wrap" FontSize="24" FontWeight="Bold" Foreground="Black"/>
                    <TextBlock Text="{Binding address}" TextWrapping="Wrap" FontSize="24" Foreground="Black" />
                </StackPanel>
            </Grid>
        </DataTemplate>
    </ListBox.ItemTemplate>
    </ListBox> 
    

    Not only does this approach allow you to keep the visual description in the more familiar Xaml fashion, its much more flexiable and resuable.

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

Sidebar

Related Questions

Here's what I want to do: Given a table PeopleOutfit (id int primary key,
Here I have an int x=3; NSLog(@%i, x); How to have it displayed like
Here an example of my checkbox list http://jsfiddle.net/YnM2f/ Let's say I check on G
Here's the flow that I am trying to achieve: 1) User uploads an audio
Here is what I am trying to achieve in PHP: I have this string:
Here's my test function (c#, visual studio 2010): [TestMethod()] public void TestGetRelevantWeeks() { List<sbyte>
Here is my problem...I have a page that loads a list of clients and
here is some C++ test code: __attribute__((visibility(hidden))) void foo() { int fd = fopen(data1,
Here's my proposed (very simplified to illustrate the problem space) design for a C#
Here the total height of all <div> 's are 900 pixels, but the jQuery

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.