Note that there are some explanatory texts on larger screens.

plurals
  1. POSet ListBox Item Background to a LinearGradientBrush depending on ItemSource Databinded Value
    primarykey
    data
    text
    <p>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</p> <p>My object in its simplified form:</p> <pre><code>public class Item { public string name { get; set; } public string address { get; set; } public int highlight { get; set; } } </code></pre> <p><strong>Converter Attempt:</strong></p> <p>Using this Converter:</p> <pre><code>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"); } } </code></pre> <p>And this Item template</p> <pre><code>&lt;ListBox Name="lstResults" ItemContainerStyle="{StaticResource ListBoxItemStyle1}"&gt; &lt;ListBox.ItemTemplate&gt; &lt;DataTemplate&gt; &lt;Grid Background="{Binding highlight, Converter={StaticResource myConverter}}"&gt; &lt;StackPanel&gt; &lt;TextBlock Text="{Binding name}" TextWrapping="Wrap" FontSize="24" FontWeight="Bold" Foreground="Black"/&gt; &lt;TextBlock Text="{Binding address}" TextWrapping="Wrap" FontSize="24" Foreground="Black" /&gt; &lt;/StackPanel&gt; &lt;/Grid&gt; &lt;/DataTemplate&gt; &lt;/ListBox.ItemTemplate&gt; &lt;/ListBox&gt; </code></pre> <p><strong>Code Behind Attempt</strong></p> <p>Added a "LinearGradientBrush background" property to my Item object</p> <pre><code>public LinearGradientBrush background { get { if (highlight == 1) return HighlightedBkg; else return NormalBkg; } } </code></pre> <p>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 :) </p> <p>Is there anyway to set the background ti a gradient from code and not using the XAML notation:</p> <pre><code>&lt;LinearGradientBrush StartPoint="0,0" EndPoint="0,1"&gt; &lt;GradientStopCollection&gt; &lt;GradientStop Color="#ff444444" Offset="0" /&gt; &lt;GradientStop Color="#ff000000" Offset="1" /&gt; &lt;/GradientStopCollection&gt; &lt;/LinearGradientBrush&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload