我正在尝试将 silverlight 3 中 RadialGradientBrush 的颜色数据绑定到属性,但似乎无法让它工作。
例如,在示例测试应用程序中,我所拥有的只是
<navigation:Page x:Class="SilverlightNavigator.HomePage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
x:Name="HomePageUC"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
Title="HomePage Page">
<Grid x:Name="LayoutRoot" Background="White">
<StackPanel>
<TextBlock
DataContext="{Binding ElementName=HomePageUC}"
Text="{Binding TestColorOne}" />
<Rectangle x:Name="testRectangle" Height="100" Width="100"
DataContext="{Binding ElementName=HomePageUC}" >
<Rectangle.Fill>
<RadialGradientBrush>
<GradientStop Color="{Binding TestColorOne}" Offset="0" />
<GradientStop Color="{Binding TestColorTwo}" Offset="1"/>
<!--
<GradientStop Color="#FFFF0000" />
<GradientStop Color="#FF00FF00" Offset="1"/>
-->
</RadialGradientBrush>
</Rectangle.Fill>
</Rectangle>
</StackPanel>
在后面的代码中,我什至使它们成为这样的依赖属性..
public static readonly DependencyProperty TestColorOneProperty =
DependencyProperty.RegisterAttached("TestColorOne", typeof(Color), typeof(HomePage), null);
public static readonly DependencyProperty TestColorTwoProperty =
DependencyProperty.RegisterAttached("TestColorTwo", typeof(Color), typeof(HomePage), null);
public Color TestColorOne
{
get { return (Color)GetValue(TestColorOneProperty); }
set { SetValue(TestColorOneProperty, value); }
}
public Color TestColorTwo
{
get { return (Color)GetValue(TestColorTwoProperty); }
set { SetValue(TestColorTwoProperty, value); }
}
但这仍然给我带来了非常无益的 AG_E_PARSER_BAD_PROPERTY_VALUE 异常。如果我取消对颜色硬编码的两行 Xaml 行的注释,它就可以正常工作。我知道这些属性很好,因为如果我对颜色进行硬编码或注释掉矩形,它就会很好地显示文本。 (通过绑定到 TextBlock)
我还尝试传递字符串“Red”、“Blue”等而不是颜色对象。但绑定似乎不起作用。
有什么建议吗?