33796

Dynamically change gradientstop color

Question:

I'm using a windows phone silverlight application, the default panorama template has a listbox with a static gradientstop property.

<ListBox ItemsSource="{Binding lbox}" Name="listboxUsers" Margin="2,126,0,6" Grid.Column="1" Grid.ColumnSpan="2" Grid.RowSpan="2"> <SNIP> <Rectangle Height="100" Width="100" Margin="12,0,9,0" Name="Recta"> <Rectangle.Fill> <LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5"> <GradientStop Color="Gray" Offset="1" /> </LinearGradientBrush> </Rectangle.Fill> </Rectangle>

I want to change the color property of the gradientstop dynamically , I tried to bind the Color property:

public class lbox { public lbox(string par1, string par2, Color par4) { displayname = par1; state = par2; gscolor = new GradientStop(); gscolor.Color = par4; } public string displayname { get; set; } public string state { get; set; } public SolidColorBrush lbcolor { get; set; } private GradientStop gscolor { get; set; } }

XAML:

GradientStop Color="{Binding gscolor}" Offset="1" />

However, this causes a XMLPaseException: AG_E_PARSER_BAD_PROPERTY_VALUE. I also tried to use a color property without a new instance of Gradientstop in the code behind... Note that the binding is working correctly for string values.

Anyone know what I'm doing wrong?

<strong>Edit:</strong> Matt pointed me in the right direction:

<Rectangle Height="100" Width="100" Margin="12,0,9,0" Fill="{Binding lbcolor}">

In the code behind as seen above, lbcolor is a SolidColorBrush.

Answer1:

This isn't possible.

See reason why at <a href="http://blogs.msdn.com/b/nickkramer/archive/2006/08/18/705116.aspx" rel="nofollow">http://blogs.msdn.com/b/nickkramer/archive/2006/08/18/705116.aspx</a>

This is discussed in depth at <a href="http://forums.silverlight.net/forums/p/111477/254754.aspx" rel="nofollow">http://forums.silverlight.net/forums/p/111477/254754.aspx</a>

There is a possible work around in the answer to <a href="https://stackoverflow.com/questions/1220688/databinding-the-color-of-a-radialgradient-brush-in-silverlight-3" rel="nofollow">Databinding the color of a RadialGradient brush in silverlight 3</a>

Answer2:

Don't you just need to set gscolor as a Color rather than GradientStop. You are assigning a GradientStop to a Color object in your XAML. Or, you should bind to the Color property of gscolor.

Color = "{Binding gscolor.Color}"

Answer3:

using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Shapes; namespace Animazioni { public partial class MainPage : UserControl { public Ellipse el = new Ellipse(); public MainPage() { InitializeComponent(); el.Opacity = 0.5; el.Height = 100; el.Width = 100; } private void UserControl_Loaded(object sender, RoutedEventArgs e) { RadialGradientBrush _rgb = new RadialGradientBrush(); GradientStop gs1 = new GradientStop(); GradientStop gs2 = new GradientStop(); GradientStop gs3 = new GradientStop(); gs1.Offset = 0; gs2.Offset = 0.7; gs3.Offset = 1; gs1.Color = Colors.Yellow; gs2.Color = Colors.Orange; gs3.Color = Colors.Red; // _rgb.GradientStops.Add(gs1); _rgb.GradientStops.Add(gs2); _rgb.GradientStops.Add(gs3); el.Fill = _rgb; myCanvas.Children.Add(el); } } }

Answer4:

I Do know that This is older than me , but i ran into the same issue. I had the Color in a DynamicResource and Storyboard animation was changing the value so the dynamicresource was getting lost. So i tried to figure out how to do it, and i ended up using Storyboard ColorAnimation programmatically.

The given Example is for 2 GradientStops

xHeader = The element the gradientstops belongs to.

And also there's a static function RandomString that is generating RandomStrings for the Storyboard name.

You would also change the PropertyPath there..

public void AnimateHeader(Color First, Color Second, Double DurationSecs=0.2) { Duration duration = new Duration(TimeSpan.FromSeconds(DurationSecs)); ColorAnimation FirstAnimation = new ColorAnimation(); ColorAnimation SecondAnimation = new ColorAnimation(); Storyboard sb = new Storyboard(); sb.Children.Add(FirstAnimation); sb.Children.Add(SecondAnimation); FirstAnimation.Duration = duration; FirstAnimation.To = First; FirstAnimation.RepeatBehavior = new RepeatBehavior(1); SecondAnimation.Duration = duration; SecondAnimation.To = Second; SecondAnimation.RepeatBehavior = new RepeatBehavior(1); Storyboard.SetTarget(FirstAnimation, xHeader); Storyboard.SetTargetProperty(FirstAnimation, new PropertyPath("Foreground.(GradientBrush.GradientStops)[0].(GradientStop.Color)")); Storyboard.SetTarget(SecondAnimation, xHeader); Storyboard.SetTargetProperty(SecondAnimation, new PropertyPath("Foreground.(GradientBrush.GradientStops)[1].(GradientStop.Color)")); Resources.Add(RandomString(100)+(new DateTime()).Ticks, sb); sb.Completed += (o, s) => { Resources.Remove(sb); }; sb.Begin(); }

Recommend

  • Change old WPF ProgressBar controltemplate to WPF4
  • ControlTemplate Opacity not displayed like I want it to
  • Solve Set property 'System.Windows.Controls.Decorator.Child' threw an exception in DataTem
  • WPF - Create reusable style with variables/parameters
  • WPF Ribbon Button glowing effects and changing image when hover
  • BorderThickness doesn't work
  • Issue to parse json
  • WPF Button Image only showing in last control
  • Delete std::shared_ptr without destroying the managed object?
  • JsonConverter: Get parent object in ReadJson, without $refs
  • How to set an entity field that does not exist on the table but does exists in the raw SQL as an ali
  • MRO with multiple inheritance in python
  • how to ignore xml namespaces?
  • Is there any way to call saveCurrentTurnWithMatchData without sending a push notification?
  • Outlook to Excel hyperlink issue
  • Efficient User-Agent Regex to find Safari in Python
  • Get Currently Active User in Android
  • What is the correct way to synchronize a shared, static object in Java?
  • Two Tables Serving as one Model in Rails
  • as3-flash: any way to access all the instances placed in different frames from document class?
  • how does System.Web.HttpRequest::PathInfo work?
  • Element.tagName for python not working
  • Spring Cloud Microservice Architecture Confusion
  • gspread or such: help me get cell coordinates (not value)
  • How can I set a binding to a Combox in a UserControl?
  • SplFileObject error failed to open stream: No such file or directory
  • print() is showing quotation marks in results
  • Play WS (2.2.1): post/put large request
  • Refering to the class itself from within a class mehod in Objective C
  • Exception “firebase.functions() takes … no argument …” when specifying a region for a Cloud Function
  • Highlight one bar in a series in highcharts?
  • Calling of Constructors in a Java
  • Use group_by to filter specific cases while keeping NAs
  • PHP: When would you need the self:: keyword?
  • log4net write single file for each call to log.info
  • Benchmarking RAM performance - UWP and C#
  • Acquiring multiple attributes from .xml file in c#
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • How can I remove ASP.NET Designer.cs files?
  • java string with new operator and a literal