【发布时间】:2009-08-03 04:44:53
【问题描述】:
我正在尝试将 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”等而不是颜色对象。但是绑定似乎不起作用。
有什么建议吗?
【问题讨论】:
标签: .net silverlight data-binding xaml