我正在使用windows phone silverlight应用程序,默认的全景模板有一个带有静态gradientstop属性的列表框。
<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>
我想动态更改gradientstop的color属性,我尝试绑定Color属性:
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" />
但是,这会导致XMLPaseException:AG_E_PARSER_BAD_PROPERTY_VALUE。我还尝试在后面的代码中使用没有新的Gradientstop实例的颜色属性...请注意,绑定对于字符串值正常工作。
任何人都知道我做错了什么?
修改 马特指出我正确的方向:
<Rectangle Height="100" Width="100" Margin="12,0,9,0" Fill="{Binding lbcolor}">
在上面的代码中,lbcolor是SolidColorBrush
。
答案 0 :(得分:4)
这是不可能的。
在http://blogs.msdn.com/b/nickkramer/archive/2006/08/18/705116.aspx
查看原因这在http://forums.silverlight.net/forums/p/111477/254754.aspx
进行了深入讨论Databinding the color of a RadialGradient brush in silverlight 3
的答案有可能解决答案 1 :(得分:1)
您不仅需要将gscolor
设置为Color
而不是GradientStop
。您正在为XAML中的GradientStop
对象分配Color
。或者,您应该绑定到Color
的{{1}}属性。
gscolor
答案 2 :(得分:0)
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);
}
}
}
答案 3 :(得分:0)
我知道这比我年长,但我遇到了同样的问题。我在DynamicResource中使用了Color,而Storyboard动画正在更改值,因此动态资源丢失了。所以我试图找出如何做到这一点,我最终以编程方式使用Storyboard ColorAnimation。
给定的示例适用于2 GradientStops
xHeader = gradientstops所属的元素。
还有一个静态函数RandomString,它为Storyboard名称生成RandomStrings。
您还可以在那里更改PropertyPath ..
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();
}